blob: 7b1eb2fcf6165f0f916b922906eff6ed7b0a267a [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 *
5 * DRM core CRTC related functions
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 *
25 * Authors:
26 * Keith Packard
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
30 */
31
32#include "drmP.h"
33#include "drm_crtc.h"
34#include "drm_crtc_helper.h"
Dave Airlied50ba252009-09-23 14:44:08 +100035#include "drm_fb_helper.h"
Dave Airlief453ba02008-11-07 14:05:41 -080036
yakui_zhao67149772009-04-02 11:52:12 +080037static void drm_mode_validate_flag(struct drm_connector *connector,
38 int flags)
39{
40 struct drm_display_mode *mode, *t;
41
42 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
43 return;
44
45 list_for_each_entry_safe(mode, t, &connector->modes, head) {
46 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
47 !(flags & DRM_MODE_FLAG_INTERLACE))
48 mode->status = MODE_NO_INTERLACE;
49 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
50 !(flags & DRM_MODE_FLAG_DBLSCAN))
51 mode->status = MODE_NO_DBLESCAN;
52 }
53
54 return;
55}
56
Dave Airlief453ba02008-11-07 14:05:41 -080057/**
Dave Airlie38651672010-03-30 05:34:13 +000058 * drm_helper_probe_single_connector_modes - get complete set of display modes
Dave Airlief453ba02008-11-07 14:05:41 -080059 * @dev: DRM device
60 * @maxX: max width for modes
61 * @maxY: max height for modes
62 *
63 * LOCKING:
64 * Caller must hold mode config lock.
65 *
66 * Based on @dev's mode_config layout, scan all the connectors and try to detect
67 * modes on them. Modes will first be added to the connector's probed_modes
68 * list, then culled (based on validity and the @maxX, @maxY parameters) and
69 * put into the normal modes list.
70 *
71 * Intended to be used either at bootup time or when major configuration
72 * changes have occurred.
73 *
74 * FIXME: take into account monitor limits
Jesse Barnes40a518d2009-01-12 12:05:32 -080075 *
76 * RETURNS:
77 * Number of modes found on @connector.
Dave Airlief453ba02008-11-07 14:05:41 -080078 */
Jesse Barnes40a518d2009-01-12 12:05:32 -080079int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
80 uint32_t maxX, uint32_t maxY)
Dave Airlief453ba02008-11-07 14:05:41 -080081{
82 struct drm_device *dev = connector->dev;
83 struct drm_display_mode *mode, *t;
84 struct drm_connector_helper_funcs *connector_funcs =
85 connector->helper_private;
Jesse Barnes40a518d2009-01-12 12:05:32 -080086 int count = 0;
yakui_zhao67149772009-04-02 11:52:12 +080087 int mode_flags = 0;
Dave Airlief453ba02008-11-07 14:05:41 -080088
Jerome Glisse94401062010-07-15 15:43:25 -040089 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
90 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -080091 /* set all modes to the unverified state */
92 list_for_each_entry_safe(mode, t, &connector->modes, head)
93 mode->status = MODE_UNVERIFIED;
94
Dave Airlied50ba252009-09-23 14:44:08 +100095 if (connector->force) {
96 if (connector->force == DRM_FORCE_ON)
97 connector->status = connector_status_connected;
98 else
99 connector->status = connector_status_disconnected;
100 if (connector->funcs->force)
101 connector->funcs->force(connector);
102 } else
103 connector->status = connector->funcs->detect(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800104
105 if (connector->status == connector_status_disconnected) {
Jerome Glisse94401062010-07-15 15:43:25 -0400106 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
107 connector->base.id, drm_get_connector_name(connector));
Zhao Yakui72539832010-03-04 08:25:55 +0000108 drm_mode_connector_update_edid_property(connector, NULL);
Adam Jackson620f3782009-09-08 11:51:46 +1000109 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800110 }
111
Jesse Barnes40a518d2009-01-12 12:05:32 -0800112 count = (*connector_funcs->get_modes)(connector);
ykzhao50fe4cf2009-09-03 14:30:04 +0800113 if (!count) {
Adam Jackson9632b412009-11-23 14:23:07 -0500114 count = drm_add_modes_noedid(connector, 1024, 768);
ykzhao50fe4cf2009-09-03 14:30:04 +0800115 if (!count)
116 return 0;
117 }
Dave Airlief453ba02008-11-07 14:05:41 -0800118
Jesse Barnes40a518d2009-01-12 12:05:32 -0800119 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800120
121 if (maxX && maxY)
122 drm_mode_validate_size(dev, &connector->modes, maxX,
123 maxY, 0);
yakui_zhao67149772009-04-02 11:52:12 +0800124
125 if (connector->interlace_allowed)
126 mode_flags |= DRM_MODE_FLAG_INTERLACE;
127 if (connector->doublescan_allowed)
128 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
129 drm_mode_validate_flag(connector, mode_flags);
130
Dave Airlief453ba02008-11-07 14:05:41 -0800131 list_for_each_entry_safe(mode, t, &connector->modes, head) {
132 if (mode->status == MODE_OK)
133 mode->status = connector_funcs->mode_valid(connector,
134 mode);
135 }
136
Adam Jackson620f3782009-09-08 11:51:46 +1000137prune:
Dave Airlief453ba02008-11-07 14:05:41 -0800138 drm_mode_prune_invalid(dev, &connector->modes, true);
139
Jesse Barnes40a518d2009-01-12 12:05:32 -0800140 if (list_empty(&connector->modes))
141 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800142
143 drm_mode_sort(&connector->modes);
144
Jerome Glisse94401062010-07-15 15:43:25 -0400145 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
146 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800147 list_for_each_entry_safe(mode, t, &connector->modes, head) {
148 mode->vrefresh = drm_mode_vrefresh(mode);
149
150 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
151 drm_mode_debug_printmodeline(mode);
152 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800153
154 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800155}
156EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
157
Dave Airlief453ba02008-11-07 14:05:41 -0800158/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700159 * drm_helper_encoder_in_use - check if a given encoder is in use
160 * @encoder: encoder to check
161 *
162 * LOCKING:
163 * Caller must hold mode config lock.
164 *
165 * Walk @encoders's DRM device's mode_config and see if it's in use.
166 *
167 * RETURNS:
168 * True if @encoder is part of the mode_config, false otherwise.
169 */
170bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
171{
172 struct drm_connector *connector;
173 struct drm_device *dev = encoder->dev;
174 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
175 if (connector->encoder == encoder)
176 return true;
177 return false;
178}
179EXPORT_SYMBOL(drm_helper_encoder_in_use);
180
181/**
Dave Airlief453ba02008-11-07 14:05:41 -0800182 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
183 * @crtc: CRTC to check
184 *
185 * LOCKING:
186 * Caller must hold mode config lock.
187 *
188 * Walk @crtc's DRM device's mode_config and see if it's in use.
189 *
190 * RETURNS:
191 * True if @crtc is part of the mode_config, false otherwise.
192 */
193bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
194{
195 struct drm_encoder *encoder;
196 struct drm_device *dev = crtc->dev;
197 /* FIXME: Locking around list access? */
198 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700199 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800200 return true;
201 return false;
202}
203EXPORT_SYMBOL(drm_helper_crtc_in_use);
204
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000205static void
206drm_encoder_disable(struct drm_encoder *encoder)
207{
208 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
209
210 if (encoder_funcs->disable)
211 (*encoder_funcs->disable)(encoder);
212 else
213 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
214}
215
Dave Airlief453ba02008-11-07 14:05:41 -0800216/**
David John89347bb2009-12-31 12:00:46 +0530217 * drm_helper_disable_unused_functions - disable unused objects
Dave Airlief453ba02008-11-07 14:05:41 -0800218 * @dev: DRM device
219 *
220 * LOCKING:
221 * Caller must hold mode config lock.
222 *
223 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
224 * by calling its dpms function, which should power it off.
225 */
226void drm_helper_disable_unused_functions(struct drm_device *dev)
227{
228 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000229 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800230 struct drm_crtc *crtc;
231
Dave Airliea3a05442009-08-31 15:16:30 +1000232 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
233 if (!connector->encoder)
234 continue;
235 if (connector->status == connector_status_disconnected)
236 connector->encoder = NULL;
237 }
238
Dave Airlief453ba02008-11-07 14:05:41 -0800239 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Dave Airliea3a05442009-08-31 15:16:30 +1000240 if (!drm_helper_encoder_in_use(encoder)) {
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000241 drm_encoder_disable(encoder);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000242 /* disconnector encoder from any connector */
243 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000244 }
Dave Airlief453ba02008-11-07 14:05:41 -0800245 }
246
247 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
248 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
249 crtc->enabled = drm_helper_crtc_in_use(crtc);
250 if (!crtc->enabled) {
Alex Deucher5c8d7172010-06-11 17:04:35 -0400251 if (crtc_funcs->disable)
252 (*crtc_funcs->disable)(crtc);
253 else
254 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
Dave Airlief453ba02008-11-07 14:05:41 -0800255 crtc->fb = NULL;
256 }
257 }
258}
259EXPORT_SYMBOL(drm_helper_disable_unused_functions);
260
Jesse Barnes7bec7562009-02-23 16:09:34 -0800261/**
262 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
263 * @encoder: encoder to test
264 * @crtc: crtc to test
265 *
266 * Return false if @encoder can't be driven by @crtc, true otherwise.
267 */
268static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
269 struct drm_crtc *crtc)
270{
271 struct drm_device *dev;
272 struct drm_crtc *tmp;
273 int crtc_mask = 1;
274
275 WARN(!crtc, "checking null crtc?");
276
277 dev = crtc->dev;
278
279 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
280 if (tmp == crtc)
281 break;
282 crtc_mask <<= 1;
283 }
284
285 if (encoder->possible_crtcs & crtc_mask)
286 return true;
287 return false;
288}
289
290/*
291 * Check the CRTC we're going to map each output to vs. its current
292 * CRTC. If they don't match, we have to disable the output and the CRTC
293 * since the driver will have to re-route things.
294 */
295static void
296drm_crtc_prepare_encoders(struct drm_device *dev)
297{
298 struct drm_encoder_helper_funcs *encoder_funcs;
299 struct drm_encoder *encoder;
300
301 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
302 encoder_funcs = encoder->helper_private;
303 /* Disable unused encoders */
304 if (encoder->crtc == NULL)
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000305 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800306 /* Disable encoders whose CRTC is about to change */
307 if (encoder_funcs->get_crtc &&
308 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000309 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800310 }
311}
312
Dave Airlief453ba02008-11-07 14:05:41 -0800313/**
314 * drm_crtc_set_mode - set a mode
315 * @crtc: CRTC to program
316 * @mode: mode to use
317 * @x: width of mode
318 * @y: height of mode
319 *
320 * LOCKING:
321 * Caller must hold mode config lock.
322 *
323 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
324 * to fixup or reject the mode prior to trying to set it.
325 *
326 * RETURNS:
327 * True if the mode was set successfully, or false otherwise.
328 */
329bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
330 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500331 int x, int y,
332 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800333{
334 struct drm_device *dev = crtc->dev;
335 struct drm_display_mode *adjusted_mode, saved_mode;
336 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
337 struct drm_encoder_helper_funcs *encoder_funcs;
338 int saved_x, saved_y;
339 struct drm_encoder *encoder;
340 bool ret = true;
341
342 adjusted_mode = drm_mode_duplicate(dev, mode);
343
344 crtc->enabled = drm_helper_crtc_in_use(crtc);
345
346 if (!crtc->enabled)
347 return true;
348
349 saved_mode = crtc->mode;
350 saved_x = crtc->x;
351 saved_y = crtc->y;
352
353 /* Update crtc values up front so the driver can rely on them for mode
354 * setting.
355 */
356 crtc->mode = *mode;
357 crtc->x = x;
358 crtc->y = y;
359
Dave Airlief453ba02008-11-07 14:05:41 -0800360 /* Pass our mode to the connectors and the CRTC to give them a chance to
361 * adjust it according to limitations or connector properties, and also
362 * a chance to reject the mode entirely.
363 */
364 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
365
366 if (encoder->crtc != crtc)
367 continue;
368 encoder_funcs = encoder->helper_private;
369 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
370 adjusted_mode))) {
371 goto done;
372 }
373 }
374
375 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
376 goto done;
377 }
Jerome Glisse94401062010-07-15 15:43:25 -0400378 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800379
380 /* Prepare the encoders and CRTCs before setting the mode. */
381 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
382
383 if (encoder->crtc != crtc)
384 continue;
385 encoder_funcs = encoder->helper_private;
386 /* Disable the encoders as the first thing we do. */
387 encoder_funcs->prepare(encoder);
388 }
389
Jesse Barnes7bec7562009-02-23 16:09:34 -0800390 drm_crtc_prepare_encoders(dev);
391
Dave Airlief453ba02008-11-07 14:05:41 -0800392 crtc_funcs->prepare(crtc);
393
394 /* Set up the DPLL and any encoders state that needs to adjust or depend
395 * on the DPLL.
396 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000397 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
398 if (!ret)
399 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800400
401 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
402
403 if (encoder->crtc != crtc)
404 continue;
405
Jerome Glisse94401062010-07-15 15:43:25 -0400406 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
407 encoder->base.id, drm_get_encoder_name(encoder),
408 mode->base.id, mode->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800409 encoder_funcs = encoder->helper_private;
410 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
411 }
412
413 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
414 crtc_funcs->commit(crtc);
415
416 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
417
418 if (encoder->crtc != crtc)
419 continue;
420
421 encoder_funcs = encoder->helper_private;
422 encoder_funcs->commit(encoder);
423
424 }
425
426 /* XXX free adjustedmode */
427 drm_mode_destroy(dev, adjusted_mode);
428 /* FIXME: add subpixel order */
429done:
430 if (!ret) {
431 crtc->mode = saved_mode;
432 crtc->x = saved_x;
433 crtc->y = saved_y;
434 }
435
436 return ret;
437}
438EXPORT_SYMBOL(drm_crtc_helper_set_mode);
439
440
441/**
442 * drm_crtc_helper_set_config - set a new config from userspace
443 * @crtc: CRTC to setup
444 * @crtc_info: user provided configuration
445 * @new_mode: new mode to set
446 * @connector_set: set of connectors for the new config
447 * @fb: new framebuffer
448 *
449 * LOCKING:
450 * Caller must hold mode config lock.
451 *
452 * Setup a new configuration, provided by the user in @crtc_info, and enable
453 * it.
454 *
455 * RETURNS:
456 * Zero. (FIXME)
457 */
458int drm_crtc_helper_set_config(struct drm_mode_set *set)
459{
460 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200461 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
462 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800463 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100464 bool mode_changed = false; /* if true do a full mode set */
465 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200466 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800467 int count = 0, ro, fail = 0;
468 struct drm_crtc_helper_funcs *crtc_funcs;
469 int ret = 0;
470
Zhao Yakui58367ed2009-07-20 13:48:07 +0800471 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800472
473 if (!set)
474 return -EINVAL;
475
476 if (!set->crtc)
477 return -EINVAL;
478
479 if (!set->crtc->helper_private)
480 return -EINVAL;
481
482 crtc_funcs = set->crtc->helper_private;
483
Jerome Glisse94401062010-07-15 15:43:25 -0400484 if (set->fb) {
485 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
486 set->crtc->base.id, set->fb->base.id,
487 (int)set->num_connectors, set->x, set->y);
488 } else {
489 DRM_DEBUG_KMS("[CRTC:%d] [NOFB] #connectors=%d (x y) (%i %i)\n",
490 set->crtc->base.id, (int)set->num_connectors,
491 set->x, set->y);
492 }
Dave Airlief453ba02008-11-07 14:05:41 -0800493
494 dev = set->crtc->dev;
495
Maarten Maathuise67aae72009-08-27 10:18:29 +0200496 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
497 * connector data. */
498 save_crtcs = kzalloc(dev->mode_config.num_crtc *
499 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800500 if (!save_crtcs)
501 return -ENOMEM;
502
Maarten Maathuise67aae72009-08-27 10:18:29 +0200503 save_encoders = kzalloc(dev->mode_config.num_encoder *
504 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800505 if (!save_encoders) {
506 kfree(save_crtcs);
507 return -ENOMEM;
508 }
509
Maarten Maathuise67aae72009-08-27 10:18:29 +0200510 save_connectors = kzalloc(dev->mode_config.num_connector *
511 sizeof(struct drm_connector), GFP_KERNEL);
512 if (!save_connectors) {
513 kfree(save_crtcs);
514 kfree(save_encoders);
515 return -ENOMEM;
516 }
517
518 /* Copy data. Note that driver private data is not affected.
519 * Should anything bad happen only the expected state is
520 * restored, not the drivers personal bookkeeping.
521 */
522 count = 0;
523 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
524 save_crtcs[count++] = *crtc;
525 }
526
527 count = 0;
528 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
529 save_encoders[count++] = *encoder;
530 }
531
532 count = 0;
533 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
534 save_connectors[count++] = *connector;
535 }
536
Dave Airlief453ba02008-11-07 14:05:41 -0800537 /* We should be able to check here if the fb has the same properties
538 * and then just flip_or_move it */
539 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800540 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800541 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800542 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800543 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100544 } else if (set->fb == NULL) {
545 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000546 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800547 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800548 }
549
550 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800551 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800552
553 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800554 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800555 drm_mode_debug_printmodeline(&set->crtc->mode);
556 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800557 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800558 }
559
560 /* a) traverse passed in connector list and get encoders for them */
561 count = 0;
562 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
563 struct drm_connector_helper_funcs *connector_funcs =
564 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800565 new_encoder = connector->encoder;
566 for (ro = 0; ro < set->num_connectors; ro++) {
567 if (set->connectors[ro] == connector) {
568 new_encoder = connector_funcs->best_encoder(connector);
569 /* if we can't get an encoder for a connector
570 we are setting now - then fail */
571 if (new_encoder == NULL)
572 /* don't break so fail path works correct */
573 fail = 1;
574 break;
575 }
576 }
577
578 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800579 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800580 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200581 /* If the encoder is reused for another connector, then
582 * the appropriate crtc will be set later.
583 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200584 if (connector->encoder)
585 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800586 connector->encoder = new_encoder;
587 }
588 }
589
590 if (fail) {
591 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200592 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800593 }
594
595 count = 0;
596 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
597 if (!connector->encoder)
598 continue;
599
Dave Airlief453ba02008-11-07 14:05:41 -0800600 if (connector->encoder->crtc == set->crtc)
601 new_crtc = NULL;
602 else
603 new_crtc = connector->encoder->crtc;
604
605 for (ro = 0; ro < set->num_connectors; ro++) {
606 if (set->connectors[ro] == connector)
607 new_crtc = set->crtc;
608 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800609
610 /* Make sure the new CRTC will work with the encoder */
611 if (new_crtc &&
612 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
613 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200614 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800615 }
Dave Airlief453ba02008-11-07 14:05:41 -0800616 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800617 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800618 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800619 connector->encoder->crtc = new_crtc;
620 }
Jerome Glisse94401062010-07-15 15:43:25 -0400621 if (new_crtc) {
622 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
623 connector->base.id, drm_get_connector_name(connector),
624 new_crtc->base.id);
625 } else {
626 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
627 connector->base.id, drm_get_connector_name(connector));
628 }
Dave Airlief453ba02008-11-07 14:05:41 -0800629 }
630
631 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800632 if (fb_changed && !crtc_funcs->mode_set_base)
633 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800634
Jesse Barnes712531b2009-01-09 13:56:14 -0800635 if (mode_changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500636 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800637 set->crtc->fb = set->fb;
638 set->crtc->enabled = (set->mode != NULL);
639 if (set->mode != NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800640 DRM_DEBUG_KMS("attempting to set mode from"
641 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800642 drm_mode_debug_printmodeline(set->mode);
643 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500644 set->x, set->y,
645 old_fb)) {
Jerome Glisse94401062010-07-15 15:43:25 -0400646 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
647 set->crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -0800648 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200649 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800650 }
Dave Airlief453ba02008-11-07 14:05:41 -0800651 }
652 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800653 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000654 set->crtc->x = set->x;
655 set->crtc->y = set->y;
656
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500657 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800658 if (set->crtc->fb != set->fb)
659 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000660 ret = crtc_funcs->mode_set_base(set->crtc,
661 set->x, set->y, old_fb);
662 if (ret != 0)
Maarten Maathuise67aae72009-08-27 10:18:29 +0200663 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800664 }
665
Maarten Maathuise67aae72009-08-27 10:18:29 +0200666 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800667 kfree(save_encoders);
668 kfree(save_crtcs);
669 return 0;
670
Maarten Maathuise67aae72009-08-27 10:18:29 +0200671fail:
672 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800673 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200674 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
675 *crtc = save_crtcs[count++];
676 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000677
Maarten Maathuise67aae72009-08-27 10:18:29 +0200678 count = 0;
679 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
680 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000681 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200682
Dave Airlief453ba02008-11-07 14:05:41 -0800683 count = 0;
684 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200685 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800686 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200687
688 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800689 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200690 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800691 return ret;
692}
693EXPORT_SYMBOL(drm_crtc_helper_set_config);
694
Keith Packardc9fb15f2009-05-30 20:42:28 -0700695static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
696{
697 int dpms = DRM_MODE_DPMS_OFF;
698 struct drm_connector *connector;
699 struct drm_device *dev = encoder->dev;
700
701 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
702 if (connector->encoder == encoder)
703 if (connector->dpms < dpms)
704 dpms = connector->dpms;
705 return dpms;
706}
707
708static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
709{
710 int dpms = DRM_MODE_DPMS_OFF;
711 struct drm_connector *connector;
712 struct drm_device *dev = crtc->dev;
713
714 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
715 if (connector->encoder && connector->encoder->crtc == crtc)
716 if (connector->dpms < dpms)
717 dpms = connector->dpms;
718 return dpms;
719}
720
721/**
722 * drm_helper_connector_dpms
723 * @connector affected connector
724 * @mode DPMS mode
725 *
726 * Calls the low-level connector DPMS function, then
727 * calls appropriate encoder and crtc DPMS functions as well
728 */
729void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
730{
731 struct drm_encoder *encoder = connector->encoder;
732 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
733 int old_dpms;
734
735 if (mode == connector->dpms)
736 return;
737
738 old_dpms = connector->dpms;
739 connector->dpms = mode;
740
741 /* from off to on, do crtc then encoder */
742 if (mode < old_dpms) {
743 if (crtc) {
744 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
745 if (crtc_funcs->dpms)
746 (*crtc_funcs->dpms) (crtc,
747 drm_helper_choose_crtc_dpms(crtc));
748 }
749 if (encoder) {
750 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
751 if (encoder_funcs->dpms)
752 (*encoder_funcs->dpms) (encoder,
753 drm_helper_choose_encoder_dpms(encoder));
754 }
755 }
756
757 /* from on to off, do encoder then crtc */
758 if (mode > old_dpms) {
759 if (encoder) {
760 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
761 if (encoder_funcs->dpms)
762 (*encoder_funcs->dpms) (encoder,
763 drm_helper_choose_encoder_dpms(encoder));
764 }
765 if (crtc) {
766 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
767 if (crtc_funcs->dpms)
768 (*crtc_funcs->dpms) (crtc,
769 drm_helper_choose_crtc_dpms(crtc));
770 }
771 }
772
773 return;
774}
775EXPORT_SYMBOL(drm_helper_connector_dpms);
776
Dave Airlief453ba02008-11-07 14:05:41 -0800777int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
778 struct drm_mode_fb_cmd *mode_cmd)
779{
780 fb->width = mode_cmd->width;
781 fb->height = mode_cmd->height;
782 fb->pitch = mode_cmd->pitch;
783 fb->bits_per_pixel = mode_cmd->bpp;
784 fb->depth = mode_cmd->depth;
785
786 return 0;
787}
788EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
789
790int drm_helper_resume_force_mode(struct drm_device *dev)
791{
792 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +0530793 struct drm_encoder *encoder;
794 struct drm_encoder_helper_funcs *encoder_funcs;
795 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800796 int ret;
797
798 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
799
800 if (!crtc->enabled)
801 continue;
802
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500803 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
804 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800805
806 if (ret == false)
807 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +0530808
809 /* Turn off outputs that were already powered off */
810 if (drm_helper_choose_crtc_dpms(crtc)) {
811 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
812
813 if(encoder->crtc != crtc)
814 continue;
815
816 encoder_funcs = encoder->helper_private;
817 if (encoder_funcs->dpms)
818 (*encoder_funcs->dpms) (encoder,
819 drm_helper_choose_encoder_dpms(encoder));
David John89347bb2009-12-31 12:00:46 +0530820 }
Chris Wilson817e6312010-08-06 15:03:31 +0100821
822 crtc_funcs = crtc->helper_private;
823 if (crtc_funcs->dpms)
824 (*crtc_funcs->dpms) (crtc,
825 drm_helper_choose_crtc_dpms(crtc));
David John89347bb2009-12-31 12:00:46 +0530826 }
Dave Airlief453ba02008-11-07 14:05:41 -0800827 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +0800828 /* disable the unused connectors while restoring the modesetting */
829 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800830 return 0;
831}
832EXPORT_SYMBOL(drm_helper_resume_force_mode);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000833
834static struct slow_work_ops output_poll_ops;
835
836#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
837static void output_poll_execute(struct slow_work *work)
838{
839 struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
840 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_slow_work);
841 struct drm_connector *connector;
842 enum drm_connector_status old_status, status;
843 bool repoll = false, changed = false;
844 int ret;
845
846 mutex_lock(&dev->mode_config.mutex);
847 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
848
849 /* if this is HPD or polled don't check it -
850 TV out for instance */
851 if (!connector->polled)
852 continue;
853
854 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
855 repoll = true;
856
857 old_status = connector->status;
858 /* if we are connected and don't want to poll for disconnect
859 skip it */
860 if (old_status == connector_status_connected &&
861 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
862 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
863 continue;
864
865 status = connector->funcs->detect(connector);
866 if (old_status != status)
867 changed = true;
868 }
869
870 mutex_unlock(&dev->mode_config.mutex);
871
872 if (changed) {
873 /* send a uevent + call fbdev */
874 drm_sysfs_hotplug_event(dev);
875 if (dev->mode_config.funcs->output_poll_changed)
876 dev->mode_config.funcs->output_poll_changed(dev);
877 }
878
879 if (repoll) {
880 ret = delayed_slow_work_enqueue(delayed_work, DRM_OUTPUT_POLL_PERIOD);
881 if (ret)
882 DRM_ERROR("delayed enqueue failed %d\n", ret);
883 }
884}
885
Dave Airliefbf81762010-06-01 09:09:06 +1000886void drm_kms_helper_poll_disable(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000887{
Dave Airliefbf81762010-06-01 09:09:06 +1000888 if (!dev->mode_config.poll_enabled)
889 return;
890 delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work);
891}
892EXPORT_SYMBOL(drm_kms_helper_poll_disable);
893
894void drm_kms_helper_poll_enable(struct drm_device *dev)
895{
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000896 bool poll = false;
Dave Airliefbf81762010-06-01 09:09:06 +1000897 struct drm_connector *connector;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000898 int ret;
899
900 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
901 if (connector->polled)
902 poll = true;
903 }
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000904
905 if (poll) {
906 ret = delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, DRM_OUTPUT_POLL_PERIOD);
907 if (ret)
908 DRM_ERROR("delayed enqueue failed %d\n", ret);
909 }
910}
Dave Airliefbf81762010-06-01 09:09:06 +1000911EXPORT_SYMBOL(drm_kms_helper_poll_enable);
912
913void drm_kms_helper_poll_init(struct drm_device *dev)
914{
915 slow_work_register_user(THIS_MODULE);
916 delayed_slow_work_init(&dev->mode_config.output_poll_slow_work,
917 &output_poll_ops);
918 dev->mode_config.poll_enabled = true;
919
920 drm_kms_helper_poll_enable(dev);
921}
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000922EXPORT_SYMBOL(drm_kms_helper_poll_init);
923
924void drm_kms_helper_poll_fini(struct drm_device *dev)
925{
Dave Airliefbf81762010-06-01 09:09:06 +1000926 drm_kms_helper_poll_disable(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000927 slow_work_unregister_user(THIS_MODULE);
928}
929EXPORT_SYMBOL(drm_kms_helper_poll_fini);
930
931void drm_helper_hpd_irq_event(struct drm_device *dev)
932{
933 if (!dev->mode_config.poll_enabled)
934 return;
935 delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work);
936 /* schedule a slow work asap */
937 delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, 0);
938}
939EXPORT_SYMBOL(drm_helper_hpd_irq_event);
940
941static struct slow_work_ops output_poll_ops = {
942 .execute = output_poll_execute,
943};