blob: 774d21e4dcddda5a5ee135ff4c9c6ade7ee63275 [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
Zhao Yakui58367ed2009-07-20 13:48:07 +080089 DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -080090 /* set all modes to the unverified state */
91 list_for_each_entry_safe(mode, t, &connector->modes, head)
92 mode->status = MODE_UNVERIFIED;
93
Dave Airlied50ba252009-09-23 14:44:08 +100094 if (connector->force) {
95 if (connector->force == DRM_FORCE_ON)
96 connector->status = connector_status_connected;
97 else
98 connector->status = connector_status_disconnected;
99 if (connector->funcs->force)
100 connector->funcs->force(connector);
101 } else
102 connector->status = connector->funcs->detect(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800103
104 if (connector->status == connector_status_disconnected) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800105 DRM_DEBUG_KMS("%s is disconnected\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800106 drm_get_connector_name(connector));
Zhao Yakui72539832010-03-04 08:25:55 +0000107 drm_mode_connector_update_edid_property(connector, NULL);
Adam Jackson620f3782009-09-08 11:51:46 +1000108 goto prune;
Dave Airlief453ba02008-11-07 14:05:41 -0800109 }
110
Jesse Barnes40a518d2009-01-12 12:05:32 -0800111 count = (*connector_funcs->get_modes)(connector);
ykzhao50fe4cf2009-09-03 14:30:04 +0800112 if (!count) {
Adam Jackson9632b412009-11-23 14:23:07 -0500113 count = drm_add_modes_noedid(connector, 1024, 768);
ykzhao50fe4cf2009-09-03 14:30:04 +0800114 if (!count)
115 return 0;
116 }
Dave Airlief453ba02008-11-07 14:05:41 -0800117
Jesse Barnes40a518d2009-01-12 12:05:32 -0800118 drm_mode_connector_list_update(connector);
Dave Airlief453ba02008-11-07 14:05:41 -0800119
120 if (maxX && maxY)
121 drm_mode_validate_size(dev, &connector->modes, maxX,
122 maxY, 0);
yakui_zhao67149772009-04-02 11:52:12 +0800123
124 if (connector->interlace_allowed)
125 mode_flags |= DRM_MODE_FLAG_INTERLACE;
126 if (connector->doublescan_allowed)
127 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
128 drm_mode_validate_flag(connector, mode_flags);
129
Dave Airlief453ba02008-11-07 14:05:41 -0800130 list_for_each_entry_safe(mode, t, &connector->modes, head) {
131 if (mode->status == MODE_OK)
132 mode->status = connector_funcs->mode_valid(connector,
133 mode);
134 }
135
Adam Jackson620f3782009-09-08 11:51:46 +1000136prune:
Dave Airlief453ba02008-11-07 14:05:41 -0800137 drm_mode_prune_invalid(dev, &connector->modes, true);
138
Jesse Barnes40a518d2009-01-12 12:05:32 -0800139 if (list_empty(&connector->modes))
140 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800141
142 drm_mode_sort(&connector->modes);
143
Zhao Yakui58367ed2009-07-20 13:48:07 +0800144 DRM_DEBUG_KMS("Probed modes for %s\n",
145 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800146 list_for_each_entry_safe(mode, t, &connector->modes, head) {
147 mode->vrefresh = drm_mode_vrefresh(mode);
148
149 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
150 drm_mode_debug_printmodeline(mode);
151 }
Jesse Barnes40a518d2009-01-12 12:05:32 -0800152
153 return count;
Dave Airlief453ba02008-11-07 14:05:41 -0800154}
155EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
156
Dave Airlief453ba02008-11-07 14:05:41 -0800157/**
Keith Packardc9fb15f2009-05-30 20:42:28 -0700158 * drm_helper_encoder_in_use - check if a given encoder is in use
159 * @encoder: encoder to check
160 *
161 * LOCKING:
162 * Caller must hold mode config lock.
163 *
164 * Walk @encoders's DRM device's mode_config and see if it's in use.
165 *
166 * RETURNS:
167 * True if @encoder is part of the mode_config, false otherwise.
168 */
169bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
170{
171 struct drm_connector *connector;
172 struct drm_device *dev = encoder->dev;
173 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
174 if (connector->encoder == encoder)
175 return true;
176 return false;
177}
178EXPORT_SYMBOL(drm_helper_encoder_in_use);
179
180/**
Dave Airlief453ba02008-11-07 14:05:41 -0800181 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
182 * @crtc: CRTC to check
183 *
184 * LOCKING:
185 * Caller must hold mode config lock.
186 *
187 * Walk @crtc's DRM device's mode_config and see if it's in use.
188 *
189 * RETURNS:
190 * True if @crtc is part of the mode_config, false otherwise.
191 */
192bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
193{
194 struct drm_encoder *encoder;
195 struct drm_device *dev = crtc->dev;
196 /* FIXME: Locking around list access? */
197 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700198 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800199 return true;
200 return false;
201}
202EXPORT_SYMBOL(drm_helper_crtc_in_use);
203
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000204static void
205drm_encoder_disable(struct drm_encoder *encoder)
206{
207 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
208
209 if (encoder_funcs->disable)
210 (*encoder_funcs->disable)(encoder);
211 else
212 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
213}
214
Dave Airlief453ba02008-11-07 14:05:41 -0800215/**
David John89347bb2009-12-31 12:00:46 +0530216 * drm_helper_disable_unused_functions - disable unused objects
Dave Airlief453ba02008-11-07 14:05:41 -0800217 * @dev: DRM device
218 *
219 * LOCKING:
220 * Caller must hold mode config lock.
221 *
222 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
223 * by calling its dpms function, which should power it off.
224 */
225void drm_helper_disable_unused_functions(struct drm_device *dev)
226{
227 struct drm_encoder *encoder;
Dave Airliea3a05442009-08-31 15:16:30 +1000228 struct drm_connector *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800229 struct drm_crtc *crtc;
230
Dave Airliea3a05442009-08-31 15:16:30 +1000231 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
232 if (!connector->encoder)
233 continue;
234 if (connector->status == connector_status_disconnected)
235 connector->encoder = NULL;
236 }
237
Dave Airlief453ba02008-11-07 14:05:41 -0800238 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Dave Airliea3a05442009-08-31 15:16:30 +1000239 if (!drm_helper_encoder_in_use(encoder)) {
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000240 drm_encoder_disable(encoder);
Dave Airlie9c552dd2009-09-02 14:00:11 +1000241 /* disconnector encoder from any connector */
242 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000243 }
Dave Airlief453ba02008-11-07 14:05:41 -0800244 }
245
246 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
247 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
248 crtc->enabled = drm_helper_crtc_in_use(crtc);
249 if (!crtc->enabled) {
Alex Deucher5c8d7172010-06-11 17:04:35 -0400250 if (crtc_funcs->disable)
251 (*crtc_funcs->disable)(crtc);
252 else
253 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
Dave Airlief453ba02008-11-07 14:05:41 -0800254 crtc->fb = NULL;
255 }
256 }
257}
258EXPORT_SYMBOL(drm_helper_disable_unused_functions);
259
Jesse Barnes7bec7562009-02-23 16:09:34 -0800260/**
261 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
262 * @encoder: encoder to test
263 * @crtc: crtc to test
264 *
265 * Return false if @encoder can't be driven by @crtc, true otherwise.
266 */
267static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
268 struct drm_crtc *crtc)
269{
270 struct drm_device *dev;
271 struct drm_crtc *tmp;
272 int crtc_mask = 1;
273
274 WARN(!crtc, "checking null crtc?");
275
276 dev = crtc->dev;
277
278 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
279 if (tmp == crtc)
280 break;
281 crtc_mask <<= 1;
282 }
283
284 if (encoder->possible_crtcs & crtc_mask)
285 return true;
286 return false;
287}
288
289/*
290 * Check the CRTC we're going to map each output to vs. its current
291 * CRTC. If they don't match, we have to disable the output and the CRTC
292 * since the driver will have to re-route things.
293 */
294static void
295drm_crtc_prepare_encoders(struct drm_device *dev)
296{
297 struct drm_encoder_helper_funcs *encoder_funcs;
298 struct drm_encoder *encoder;
299
300 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
301 encoder_funcs = encoder->helper_private;
302 /* Disable unused encoders */
303 if (encoder->crtc == NULL)
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000304 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800305 /* Disable encoders whose CRTC is about to change */
306 if (encoder_funcs->get_crtc &&
307 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
Ben Skeggs86a1b9d2010-07-01 16:49:57 +1000308 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800309 }
310}
311
Dave Airlief453ba02008-11-07 14:05:41 -0800312/**
313 * drm_crtc_set_mode - set a mode
314 * @crtc: CRTC to program
315 * @mode: mode to use
316 * @x: width of mode
317 * @y: height of mode
318 *
319 * LOCKING:
320 * Caller must hold mode config lock.
321 *
322 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
323 * to fixup or reject the mode prior to trying to set it.
324 *
325 * RETURNS:
326 * True if the mode was set successfully, or false otherwise.
327 */
328bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
329 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500330 int x, int y,
331 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800332{
333 struct drm_device *dev = crtc->dev;
334 struct drm_display_mode *adjusted_mode, saved_mode;
335 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
336 struct drm_encoder_helper_funcs *encoder_funcs;
337 int saved_x, saved_y;
338 struct drm_encoder *encoder;
339 bool ret = true;
340
341 adjusted_mode = drm_mode_duplicate(dev, mode);
342
343 crtc->enabled = drm_helper_crtc_in_use(crtc);
344
345 if (!crtc->enabled)
346 return true;
347
348 saved_mode = crtc->mode;
349 saved_x = crtc->x;
350 saved_y = crtc->y;
351
352 /* Update crtc values up front so the driver can rely on them for mode
353 * setting.
354 */
355 crtc->mode = *mode;
356 crtc->x = x;
357 crtc->y = y;
358
Dave Airlief453ba02008-11-07 14:05:41 -0800359 /* Pass our mode to the connectors and the CRTC to give them a chance to
360 * adjust it according to limitations or connector properties, and also
361 * a chance to reject the mode entirely.
362 */
363 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
364
365 if (encoder->crtc != crtc)
366 continue;
367 encoder_funcs = encoder->helper_private;
368 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
369 adjusted_mode))) {
370 goto done;
371 }
372 }
373
374 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
375 goto done;
376 }
377
378 /* Prepare the encoders and CRTCs before setting the mode. */
379 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
380
381 if (encoder->crtc != crtc)
382 continue;
383 encoder_funcs = encoder->helper_private;
384 /* Disable the encoders as the first thing we do. */
385 encoder_funcs->prepare(encoder);
386 }
387
Jesse Barnes7bec7562009-02-23 16:09:34 -0800388 drm_crtc_prepare_encoders(dev);
389
Dave Airlief453ba02008-11-07 14:05:41 -0800390 crtc_funcs->prepare(crtc);
391
392 /* Set up the DPLL and any encoders state that needs to adjust or depend
393 * on the DPLL.
394 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000395 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
396 if (!ret)
397 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800398
399 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
400
401 if (encoder->crtc != crtc)
402 continue;
403
Dave Youngef145872010-01-13 13:38:59 +0800404 DRM_DEBUG("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
Dave Airlief453ba02008-11-07 14:05:41 -0800405 mode->name, mode->base.id);
406 encoder_funcs = encoder->helper_private;
407 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
408 }
409
410 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
411 crtc_funcs->commit(crtc);
412
413 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
414
415 if (encoder->crtc != crtc)
416 continue;
417
418 encoder_funcs = encoder->helper_private;
419 encoder_funcs->commit(encoder);
420
421 }
422
423 /* XXX free adjustedmode */
424 drm_mode_destroy(dev, adjusted_mode);
425 /* FIXME: add subpixel order */
426done:
427 if (!ret) {
428 crtc->mode = saved_mode;
429 crtc->x = saved_x;
430 crtc->y = saved_y;
431 }
432
433 return ret;
434}
435EXPORT_SYMBOL(drm_crtc_helper_set_mode);
436
437
438/**
439 * drm_crtc_helper_set_config - set a new config from userspace
440 * @crtc: CRTC to setup
441 * @crtc_info: user provided configuration
442 * @new_mode: new mode to set
443 * @connector_set: set of connectors for the new config
444 * @fb: new framebuffer
445 *
446 * LOCKING:
447 * Caller must hold mode config lock.
448 *
449 * Setup a new configuration, provided by the user in @crtc_info, and enable
450 * it.
451 *
452 * RETURNS:
453 * Zero. (FIXME)
454 */
455int drm_crtc_helper_set_config(struct drm_mode_set *set)
456{
457 struct drm_device *dev;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200458 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
459 struct drm_encoder *save_encoders, *new_encoder, *encoder;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800460 struct drm_framebuffer *old_fb = NULL;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100461 bool mode_changed = false; /* if true do a full mode set */
462 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Maarten Maathuise67aae72009-08-27 10:18:29 +0200463 struct drm_connector *save_connectors, *connector;
Dave Airlief453ba02008-11-07 14:05:41 -0800464 int count = 0, ro, fail = 0;
465 struct drm_crtc_helper_funcs *crtc_funcs;
466 int ret = 0;
467
Zhao Yakui58367ed2009-07-20 13:48:07 +0800468 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800469
470 if (!set)
471 return -EINVAL;
472
473 if (!set->crtc)
474 return -EINVAL;
475
476 if (!set->crtc->helper_private)
477 return -EINVAL;
478
479 crtc_funcs = set->crtc->helper_private;
480
Zhao Yakui58367ed2009-07-20 13:48:07 +0800481 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
482 " %d (x, y) (%i, %i)\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800483 set->crtc, set->crtc->base.id, set->fb, set->connectors,
484 (int)set->num_connectors, set->x, set->y);
485
486 dev = set->crtc->dev;
487
Maarten Maathuise67aae72009-08-27 10:18:29 +0200488 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
489 * connector data. */
490 save_crtcs = kzalloc(dev->mode_config.num_crtc *
491 sizeof(struct drm_crtc), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800492 if (!save_crtcs)
493 return -ENOMEM;
494
Maarten Maathuise67aae72009-08-27 10:18:29 +0200495 save_encoders = kzalloc(dev->mode_config.num_encoder *
496 sizeof(struct drm_encoder), GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -0800497 if (!save_encoders) {
498 kfree(save_crtcs);
499 return -ENOMEM;
500 }
501
Maarten Maathuise67aae72009-08-27 10:18:29 +0200502 save_connectors = kzalloc(dev->mode_config.num_connector *
503 sizeof(struct drm_connector), GFP_KERNEL);
504 if (!save_connectors) {
505 kfree(save_crtcs);
506 kfree(save_encoders);
507 return -ENOMEM;
508 }
509
510 /* Copy data. Note that driver private data is not affected.
511 * Should anything bad happen only the expected state is
512 * restored, not the drivers personal bookkeeping.
513 */
514 count = 0;
515 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
516 save_crtcs[count++] = *crtc;
517 }
518
519 count = 0;
520 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
521 save_encoders[count++] = *encoder;
522 }
523
524 count = 0;
525 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
526 save_connectors[count++] = *connector;
527 }
528
Dave Airlief453ba02008-11-07 14:05:41 -0800529 /* We should be able to check here if the fb has the same properties
530 * and then just flip_or_move it */
531 if (set->crtc->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800532 /* If we have no fb then treat it as a full mode set */
Jesse Barnes7bec7562009-02-23 16:09:34 -0800533 if (set->crtc->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800534 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800535 mode_changed = true;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100536 } else if (set->fb == NULL) {
537 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000538 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800539 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800540 }
541
542 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800543 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800544
545 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800546 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800547 drm_mode_debug_printmodeline(&set->crtc->mode);
548 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800549 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800550 }
551
552 /* a) traverse passed in connector list and get encoders for them */
553 count = 0;
554 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
555 struct drm_connector_helper_funcs *connector_funcs =
556 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800557 new_encoder = connector->encoder;
558 for (ro = 0; ro < set->num_connectors; ro++) {
559 if (set->connectors[ro] == connector) {
560 new_encoder = connector_funcs->best_encoder(connector);
561 /* if we can't get an encoder for a connector
562 we are setting now - then fail */
563 if (new_encoder == NULL)
564 /* don't break so fail path works correct */
565 fail = 1;
566 break;
567 }
568 }
569
570 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800571 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800572 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200573 /* If the encoder is reused for another connector, then
574 * the appropriate crtc will be set later.
575 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200576 if (connector->encoder)
577 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800578 connector->encoder = new_encoder;
579 }
580 }
581
582 if (fail) {
583 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200584 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800585 }
586
587 count = 0;
588 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
589 if (!connector->encoder)
590 continue;
591
Dave Airlief453ba02008-11-07 14:05:41 -0800592 if (connector->encoder->crtc == set->crtc)
593 new_crtc = NULL;
594 else
595 new_crtc = connector->encoder->crtc;
596
597 for (ro = 0; ro < set->num_connectors; ro++) {
598 if (set->connectors[ro] == connector)
599 new_crtc = set->crtc;
600 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800601
602 /* Make sure the new CRTC will work with the encoder */
603 if (new_crtc &&
604 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
605 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200606 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800607 }
Dave Airlief453ba02008-11-07 14:05:41 -0800608 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800609 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800610 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800611 connector->encoder->crtc = new_crtc;
612 }
Zhao Yakui58367ed2009-07-20 13:48:07 +0800613 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
Jesse Barnes7bec7562009-02-23 16:09:34 -0800614 connector->base.id, new_crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800615 }
616
617 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800618 if (fb_changed && !crtc_funcs->mode_set_base)
619 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800620
Jesse Barnes712531b2009-01-09 13:56:14 -0800621 if (mode_changed) {
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500622 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800623 set->crtc->fb = set->fb;
624 set->crtc->enabled = (set->mode != NULL);
625 if (set->mode != NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800626 DRM_DEBUG_KMS("attempting to set mode from"
627 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800628 drm_mode_debug_printmodeline(set->mode);
629 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500630 set->x, set->y,
631 old_fb)) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000632 DRM_ERROR("failed to set mode on crtc %p\n",
633 set->crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800634 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200635 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800636 }
Dave Airlief453ba02008-11-07 14:05:41 -0800637 }
638 drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800639 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000640 set->crtc->x = set->x;
641 set->crtc->y = set->y;
642
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500643 old_fb = set->crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800644 if (set->crtc->fb != set->fb)
645 set->crtc->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000646 ret = crtc_funcs->mode_set_base(set->crtc,
647 set->x, set->y, old_fb);
648 if (ret != 0)
Maarten Maathuise67aae72009-08-27 10:18:29 +0200649 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800650 }
651
Maarten Maathuise67aae72009-08-27 10:18:29 +0200652 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800653 kfree(save_encoders);
654 kfree(save_crtcs);
655 return 0;
656
Maarten Maathuise67aae72009-08-27 10:18:29 +0200657fail:
658 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800659 count = 0;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200660 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
661 *crtc = save_crtcs[count++];
662 }
Chris Wilsone62fb642009-02-11 16:39:21 +0000663
Maarten Maathuise67aae72009-08-27 10:18:29 +0200664 count = 0;
665 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
666 *encoder = save_encoders[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000667 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200668
Dave Airlief453ba02008-11-07 14:05:41 -0800669 count = 0;
670 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Maarten Maathuise67aae72009-08-27 10:18:29 +0200671 *connector = save_connectors[count++];
Dave Airlief453ba02008-11-07 14:05:41 -0800672 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200673
674 kfree(save_connectors);
Dave Airlief453ba02008-11-07 14:05:41 -0800675 kfree(save_encoders);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200676 kfree(save_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800677 return ret;
678}
679EXPORT_SYMBOL(drm_crtc_helper_set_config);
680
Keith Packardc9fb15f2009-05-30 20:42:28 -0700681static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
682{
683 int dpms = DRM_MODE_DPMS_OFF;
684 struct drm_connector *connector;
685 struct drm_device *dev = encoder->dev;
686
687 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
688 if (connector->encoder == encoder)
689 if (connector->dpms < dpms)
690 dpms = connector->dpms;
691 return dpms;
692}
693
694static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
695{
696 int dpms = DRM_MODE_DPMS_OFF;
697 struct drm_connector *connector;
698 struct drm_device *dev = crtc->dev;
699
700 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
701 if (connector->encoder && connector->encoder->crtc == crtc)
702 if (connector->dpms < dpms)
703 dpms = connector->dpms;
704 return dpms;
705}
706
707/**
708 * drm_helper_connector_dpms
709 * @connector affected connector
710 * @mode DPMS mode
711 *
712 * Calls the low-level connector DPMS function, then
713 * calls appropriate encoder and crtc DPMS functions as well
714 */
715void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
716{
717 struct drm_encoder *encoder = connector->encoder;
718 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
719 int old_dpms;
720
721 if (mode == connector->dpms)
722 return;
723
724 old_dpms = connector->dpms;
725 connector->dpms = mode;
726
727 /* from off to on, do crtc then encoder */
728 if (mode < old_dpms) {
729 if (crtc) {
730 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
731 if (crtc_funcs->dpms)
732 (*crtc_funcs->dpms) (crtc,
733 drm_helper_choose_crtc_dpms(crtc));
734 }
735 if (encoder) {
736 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
737 if (encoder_funcs->dpms)
738 (*encoder_funcs->dpms) (encoder,
739 drm_helper_choose_encoder_dpms(encoder));
740 }
741 }
742
743 /* from on to off, do encoder then crtc */
744 if (mode > old_dpms) {
745 if (encoder) {
746 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
747 if (encoder_funcs->dpms)
748 (*encoder_funcs->dpms) (encoder,
749 drm_helper_choose_encoder_dpms(encoder));
750 }
751 if (crtc) {
752 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
753 if (crtc_funcs->dpms)
754 (*crtc_funcs->dpms) (crtc,
755 drm_helper_choose_crtc_dpms(crtc));
756 }
757 }
758
759 return;
760}
761EXPORT_SYMBOL(drm_helper_connector_dpms);
762
Dave Airlief453ba02008-11-07 14:05:41 -0800763int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
764 struct drm_mode_fb_cmd *mode_cmd)
765{
766 fb->width = mode_cmd->width;
767 fb->height = mode_cmd->height;
768 fb->pitch = mode_cmd->pitch;
769 fb->bits_per_pixel = mode_cmd->bpp;
770 fb->depth = mode_cmd->depth;
771
772 return 0;
773}
774EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
775
776int drm_helper_resume_force_mode(struct drm_device *dev)
777{
778 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +0530779 struct drm_encoder *encoder;
780 struct drm_encoder_helper_funcs *encoder_funcs;
781 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800782 int ret;
783
784 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
785
786 if (!crtc->enabled)
787 continue;
788
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500789 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
790 crtc->x, crtc->y, crtc->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800791
792 if (ret == false)
793 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +0530794
795 /* Turn off outputs that were already powered off */
796 if (drm_helper_choose_crtc_dpms(crtc)) {
797 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
798
799 if(encoder->crtc != crtc)
800 continue;
801
802 encoder_funcs = encoder->helper_private;
803 if (encoder_funcs->dpms)
804 (*encoder_funcs->dpms) (encoder,
805 drm_helper_choose_encoder_dpms(encoder));
806
807 crtc_funcs = crtc->helper_private;
808 if (crtc_funcs->dpms)
809 (*crtc_funcs->dpms) (crtc,
810 drm_helper_choose_crtc_dpms(crtc));
811 }
812 }
Dave Airlief453ba02008-11-07 14:05:41 -0800813 }
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +0800814 /* disable the unused connectors while restoring the modesetting */
815 drm_helper_disable_unused_functions(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800816 return 0;
817}
818EXPORT_SYMBOL(drm_helper_resume_force_mode);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000819
820static struct slow_work_ops output_poll_ops;
821
822#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
823static void output_poll_execute(struct slow_work *work)
824{
825 struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
826 struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_slow_work);
827 struct drm_connector *connector;
828 enum drm_connector_status old_status, status;
829 bool repoll = false, changed = false;
830 int ret;
831
832 mutex_lock(&dev->mode_config.mutex);
833 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
834
835 /* if this is HPD or polled don't check it -
836 TV out for instance */
837 if (!connector->polled)
838 continue;
839
840 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT))
841 repoll = true;
842
843 old_status = connector->status;
844 /* if we are connected and don't want to poll for disconnect
845 skip it */
846 if (old_status == connector_status_connected &&
847 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) &&
848 !(connector->polled & DRM_CONNECTOR_POLL_HPD))
849 continue;
850
851 status = connector->funcs->detect(connector);
852 if (old_status != status)
853 changed = true;
854 }
855
856 mutex_unlock(&dev->mode_config.mutex);
857
858 if (changed) {
859 /* send a uevent + call fbdev */
860 drm_sysfs_hotplug_event(dev);
861 if (dev->mode_config.funcs->output_poll_changed)
862 dev->mode_config.funcs->output_poll_changed(dev);
863 }
864
865 if (repoll) {
866 ret = delayed_slow_work_enqueue(delayed_work, DRM_OUTPUT_POLL_PERIOD);
867 if (ret)
868 DRM_ERROR("delayed enqueue failed %d\n", ret);
869 }
870}
871
Dave Airliefbf81762010-06-01 09:09:06 +1000872void drm_kms_helper_poll_disable(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000873{
Dave Airliefbf81762010-06-01 09:09:06 +1000874 if (!dev->mode_config.poll_enabled)
875 return;
876 delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work);
877}
878EXPORT_SYMBOL(drm_kms_helper_poll_disable);
879
880void drm_kms_helper_poll_enable(struct drm_device *dev)
881{
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000882 bool poll = false;
Dave Airliefbf81762010-06-01 09:09:06 +1000883 struct drm_connector *connector;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000884 int ret;
885
886 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
887 if (connector->polled)
888 poll = true;
889 }
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000890
891 if (poll) {
892 ret = delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, DRM_OUTPUT_POLL_PERIOD);
893 if (ret)
894 DRM_ERROR("delayed enqueue failed %d\n", ret);
895 }
896}
Dave Airliefbf81762010-06-01 09:09:06 +1000897EXPORT_SYMBOL(drm_kms_helper_poll_enable);
898
899void drm_kms_helper_poll_init(struct drm_device *dev)
900{
901 slow_work_register_user(THIS_MODULE);
902 delayed_slow_work_init(&dev->mode_config.output_poll_slow_work,
903 &output_poll_ops);
904 dev->mode_config.poll_enabled = true;
905
906 drm_kms_helper_poll_enable(dev);
907}
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000908EXPORT_SYMBOL(drm_kms_helper_poll_init);
909
910void drm_kms_helper_poll_fini(struct drm_device *dev)
911{
Dave Airliefbf81762010-06-01 09:09:06 +1000912 drm_kms_helper_poll_disable(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000913 slow_work_unregister_user(THIS_MODULE);
914}
915EXPORT_SYMBOL(drm_kms_helper_poll_fini);
916
917void drm_helper_hpd_irq_event(struct drm_device *dev)
918{
919 if (!dev->mode_config.poll_enabled)
920 return;
921 delayed_slow_work_cancel(&dev->mode_config.output_poll_slow_work);
922 /* schedule a slow work asap */
923 delayed_slow_work_enqueue(&dev->mode_config.output_poll_slow_work, 0);
924}
925EXPORT_SYMBOL(drm_helper_hpd_irq_event);
926
927static struct slow_work_ops output_poll_ops = {
928 .execute = output_poll_execute,
929};