2 * Copyright (c) 2016 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_fb_helper.h>
26 #include <drm/drm_modeset_helper.h>
27 #include <drm/drm_plane_helper.h>
30 * DOC: aux kms helpers
32 * This helper library contains various one-off functions which don't really fit
33 * anywhere else in the DRM modeset helper library.
37 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
39 * @dev: drm device to operate on
41 * Some userspace presumes that the first connected connector is the main
42 * display, where it's supposed to display e.g. the login screen. For
43 * laptops, this should be the main panel. Use this function to sort all
44 * (eDP/LVDS/DSI) panels to the front of the connector list, instead of
45 * painstakingly trying to initialize them in the right order.
47 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
49 struct drm_connector *connector, *tmp;
50 struct list_head panel_list;
52 INIT_LIST_HEAD(&panel_list);
54 spin_lock_irq(&dev->mode_config.connector_list_lock);
55 list_for_each_entry_safe(connector, tmp,
56 &dev->mode_config.connector_list, head) {
57 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
58 connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
59 connector->connector_type == DRM_MODE_CONNECTOR_DSI)
60 list_move_tail(&connector->head, &panel_list);
63 list_splice(&panel_list, &dev->mode_config.connector_list);
64 spin_unlock_irq(&dev->mode_config.connector_list_lock);
66 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
69 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
71 * @fb: drm_framebuffer object to fill out
72 * @mode_cmd: metadata from the userspace fb creation request
74 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
77 void drm_helper_mode_fill_fb_struct(struct drm_device *dev,
78 struct drm_framebuffer *fb,
79 const struct drm_mode_fb_cmd2 *mode_cmd)
84 fb->format = drm_get_format_info(dev, mode_cmd);
85 fb->width = mode_cmd->width;
86 fb->height = mode_cmd->height;
87 for (i = 0; i < 4; i++) {
88 fb->pitches[i] = mode_cmd->pitches[i];
89 fb->offsets[i] = mode_cmd->offsets[i];
91 fb->modifier = mode_cmd->modifier[0];
92 fb->flags = mode_cmd->flags;
94 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
97 * This is the minimal list of formats that seem to be safe for modeset use
98 * with all current DRM drivers. Most hardware can actually support more
99 * formats than this and drivers may specify a more accurate list when
100 * creating the primary plane. However drivers that still call
101 * drm_plane_init() will use this minimal format list as the default.
103 static const uint32_t safe_modeset_formats[] = {
108 static struct drm_plane *create_primary_plane(struct drm_device *dev)
110 struct drm_plane *primary;
113 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
114 if (primary == NULL) {
115 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
120 * Remove the format_default field from drm_plane when dropping
123 primary->format_default = true;
125 /* possible_crtc's will be filled in later by crtc_init */
126 ret = drm_universal_plane_init(dev, primary, 0,
127 &drm_primary_helper_funcs,
128 safe_modeset_formats,
129 ARRAY_SIZE(safe_modeset_formats),
131 DRM_PLANE_TYPE_PRIMARY, NULL);
141 * drm_crtc_init - Legacy CRTC initialization function
143 * @crtc: CRTC object to init
144 * @funcs: callbacks for the new CRTC
146 * Initialize a CRTC object with a default helper-provided primary plane and no
149 * Note that we make some assumptions about hardware limitations that may not be
150 * true for all hardware:
152 * 1. Primary plane cannot be repositioned.
153 * 2. Primary plane cannot be scaled.
154 * 3. Primary plane must cover the entire CRTC.
155 * 4. Subpixel positioning is not supported.
156 * 5. The primary plane must always be on if the CRTC is enabled.
158 * This is purely a backwards compatibility helper for old drivers. Drivers
159 * should instead implement their own primary plane. Atomic drivers must do so.
160 * Drivers with the above hardware restriction can look into using &struct
161 * drm_simple_display_pipe, which encapsulates the above limitations into a nice
165 * Zero on success, error code on failure.
167 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
168 const struct drm_crtc_funcs *funcs)
170 struct drm_plane *primary;
172 primary = create_primary_plane(dev);
173 return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs,
176 EXPORT_SYMBOL(drm_crtc_init);
179 * drm_mode_config_helper_suspend - Modeset suspend helper
182 * This helper function takes care of suspending the modeset side. It disables
183 * output polling if initialized, suspends fbdev if used and finally calls
184 * drm_atomic_helper_suspend().
185 * If suspending fails, fbdev and polling is re-enabled.
188 * Zero on success, negative error code on error.
191 * drm_kms_helper_poll_disable() and drm_fb_helper_set_suspend_unlocked().
193 int drm_mode_config_helper_suspend(struct drm_device *dev)
195 struct drm_atomic_state *state;
200 drm_kms_helper_poll_disable(dev);
201 drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 1);
202 state = drm_atomic_helper_suspend(dev);
204 drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
205 drm_kms_helper_poll_enable(dev);
206 return PTR_ERR(state);
209 dev->mode_config.suspend_state = state;
213 EXPORT_SYMBOL(drm_mode_config_helper_suspend);
216 * drm_mode_config_helper_resume - Modeset resume helper
219 * This helper function takes care of resuming the modeset side. It calls
220 * drm_atomic_helper_resume(), resumes fbdev if used and enables output polling
224 * Zero on success, negative error code on error.
227 * drm_fb_helper_set_suspend_unlocked() and drm_kms_helper_poll_enable().
229 int drm_mode_config_helper_resume(struct drm_device *dev)
236 if (WARN_ON(!dev->mode_config.suspend_state))
239 ret = drm_atomic_helper_resume(dev, dev->mode_config.suspend_state);
241 DRM_ERROR("Failed to resume (%d)\n", ret);
242 dev->mode_config.suspend_state = NULL;
244 drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
245 drm_kms_helper_poll_enable(dev);
249 EXPORT_SYMBOL(drm_mode_config_helper_resume);