blob: 57a319e3f78087faea7649224796deb4d5e633c0 [file] [log] [blame]
Daniel Vetter1de72fa2016-08-12 22:48:39 +02001/*
2 * Copyright (c) 2016 Intel Corporation
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23#include <drm/drm_modeset_helper.h>
24#include <drm/drm_plane_helper.h>
25
26/**
27 * DOC: aux kms helpers
28 *
29 * This helper library contains various one-off functions which don't really fit
30 * anywhere else in the DRM modeset helper library.
31 */
32
33/**
34 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
35 * connector list
36 * @dev: drm device to operate on
37 *
38 * Some userspace presumes that the first connected connector is the main
39 * display, where it's supposed to display e.g. the login screen. For
40 * laptops, this should be the main panel. Use this function to sort all
Jani Nikula27bfa742016-11-17 12:29:08 +020041 * (eDP/LVDS/DSI) panels to the front of the connector list, instead of
Daniel Vetter1de72fa2016-08-12 22:48:39 +020042 * painstakingly trying to initialize them in the right order.
43 */
44void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
45{
46 struct drm_connector *connector, *tmp;
47 struct list_head panel_list;
48
49 INIT_LIST_HEAD(&panel_list);
50
51 list_for_each_entry_safe(connector, tmp,
52 &dev->mode_config.connector_list, head) {
53 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
Jani Nikula27bfa742016-11-17 12:29:08 +020054 connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
55 connector->connector_type == DRM_MODE_CONNECTOR_DSI)
Daniel Vetter1de72fa2016-08-12 22:48:39 +020056 list_move_tail(&connector->head, &panel_list);
57 }
58
59 list_splice(&panel_list, &dev->mode_config.connector_list);
60}
61EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
62
63/**
64 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
65 * @fb: drm_framebuffer object to fill out
66 * @mode_cmd: metadata from the userspace fb creation request
67 *
68 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
69 * metadata fields.
70 */
Ville Syrjäläa3f913c2016-12-14 22:48:59 +020071void drm_helper_mode_fill_fb_struct(struct drm_device *dev,
72 struct drm_framebuffer *fb,
Daniel Vetter1de72fa2016-08-12 22:48:39 +020073 const struct drm_mode_fb_cmd2 *mode_cmd)
74{
Laurent Pinchart488546f2016-10-18 01:41:21 +030075 const struct drm_format_info *info;
Daniel Vetter1de72fa2016-08-12 22:48:39 +020076 int i;
77
Laurent Pinchart488546f2016-10-18 01:41:21 +030078 info = drm_format_info(mode_cmd->pixel_format);
79 if (!info || !info->depth) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +000080 struct drm_format_name_buf format_name;
Laurent Pinchart488546f2016-10-18 01:41:21 +030081
Eric Engestromb3c11ac2016-11-12 01:12:56 +000082 DRM_DEBUG_KMS("non-RGB pixel format %s\n",
83 drm_get_format_name(mode_cmd->pixel_format,
84 &format_name));
Laurent Pinchart488546f2016-10-18 01:41:21 +030085
86 fb->depth = 0;
87 fb->bits_per_pixel = 0;
88 } else {
89 fb->depth = info->depth;
90 fb->bits_per_pixel = info->cpp[0] * 8;
91 }
92
Ville Syrjälä95bce762016-11-18 21:52:54 +020093 fb->dev = dev;
Daniel Vetter1de72fa2016-08-12 22:48:39 +020094 fb->width = mode_cmd->width;
95 fb->height = mode_cmd->height;
96 for (i = 0; i < 4; i++) {
97 fb->pitches[i] = mode_cmd->pitches[i];
98 fb->offsets[i] = mode_cmd->offsets[i];
Daniel Vetter1de72fa2016-08-12 22:48:39 +020099 }
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200100 fb->modifier = mode_cmd->modifier[0];
Daniel Vetter1de72fa2016-08-12 22:48:39 +0200101 fb->pixel_format = mode_cmd->pixel_format;
102 fb->flags = mode_cmd->flags;
103}
104EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
105
106/*
107 * This is the minimal list of formats that seem to be safe for modeset use
108 * with all current DRM drivers. Most hardware can actually support more
109 * formats than this and drivers may specify a more accurate list when
110 * creating the primary plane. However drivers that still call
111 * drm_plane_init() will use this minimal format list as the default.
112 */
113static const uint32_t safe_modeset_formats[] = {
114 DRM_FORMAT_XRGB8888,
115 DRM_FORMAT_ARGB8888,
116};
117
118static struct drm_plane *create_primary_plane(struct drm_device *dev)
119{
120 struct drm_plane *primary;
121 int ret;
122
123 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
124 if (primary == NULL) {
125 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
126 return NULL;
127 }
128
129 /*
130 * Remove the format_default field from drm_plane when dropping
131 * this helper.
132 */
133 primary->format_default = true;
134
135 /* possible_crtc's will be filled in later by crtc_init */
136 ret = drm_universal_plane_init(dev, primary, 0,
137 &drm_primary_helper_funcs,
138 safe_modeset_formats,
139 ARRAY_SIZE(safe_modeset_formats),
140 DRM_PLANE_TYPE_PRIMARY, NULL);
141 if (ret) {
142 kfree(primary);
143 primary = NULL;
144 }
145
146 return primary;
147}
148
149/**
150 * drm_crtc_init - Legacy CRTC initialization function
151 * @dev: DRM device
152 * @crtc: CRTC object to init
153 * @funcs: callbacks for the new CRTC
154 *
155 * Initialize a CRTC object with a default helper-provided primary plane and no
156 * cursor plane.
157 *
158 * Returns:
159 * Zero on success, error code on failure.
160 */
161int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
162 const struct drm_crtc_funcs *funcs)
163{
164 struct drm_plane *primary;
165
166 primary = create_primary_plane(dev);
167 return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs,
168 NULL);
169}
170EXPORT_SYMBOL(drm_crtc_init);