]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/drm_modeset_helper.c
Merge tag 'drm-misc-next-2016-11-16' of git://anongit.freedesktop.org/git/drm-misc...
[karo-tx-linux.git] / drivers / gpu / drm / drm_modeset_helper.c
1 /*
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
41  * (eDP/LVDS) panels to the front of the connector list, instead of
42  * painstakingly trying to initialize them in the right order.
43  */
44 void 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 ||
54                     connector->connector_type == DRM_MODE_CONNECTOR_eDP)
55                         list_move_tail(&connector->head, &panel_list);
56         }
57
58         list_splice(&panel_list, &dev->mode_config.connector_list);
59 }
60 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
61
62 /**
63  * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
64  * @fb: drm_framebuffer object to fill out
65  * @mode_cmd: metadata from the userspace fb creation request
66  *
67  * This helper can be used in a drivers fb_create callback to pre-fill the fb's
68  * metadata fields.
69  */
70 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
71                                     const struct drm_mode_fb_cmd2 *mode_cmd)
72 {
73         const struct drm_format_info *info;
74         int i;
75
76         info = drm_format_info(mode_cmd->pixel_format);
77         if (!info || !info->depth) {
78                 struct drm_format_name_buf format_name;
79
80                 DRM_DEBUG_KMS("non-RGB pixel format %s\n",
81                               drm_get_format_name(mode_cmd->pixel_format,
82                                                   &format_name));
83
84                 fb->depth = 0;
85                 fb->bits_per_pixel = 0;
86         } else {
87                 fb->depth = info->depth;
88                 fb->bits_per_pixel = info->cpp[0] * 8;
89         }
90
91         fb->width = mode_cmd->width;
92         fb->height = mode_cmd->height;
93         for (i = 0; i < 4; i++) {
94                 fb->pitches[i] = mode_cmd->pitches[i];
95                 fb->offsets[i] = mode_cmd->offsets[i];
96                 fb->modifier[i] = mode_cmd->modifier[i];
97         }
98         fb->pixel_format = mode_cmd->pixel_format;
99         fb->flags = mode_cmd->flags;
100 }
101 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
102
103 /*
104  * This is the minimal list of formats that seem to be safe for modeset use
105  * with all current DRM drivers.  Most hardware can actually support more
106  * formats than this and drivers may specify a more accurate list when
107  * creating the primary plane.  However drivers that still call
108  * drm_plane_init() will use this minimal format list as the default.
109  */
110 static const uint32_t safe_modeset_formats[] = {
111         DRM_FORMAT_XRGB8888,
112         DRM_FORMAT_ARGB8888,
113 };
114
115 static struct drm_plane *create_primary_plane(struct drm_device *dev)
116 {
117         struct drm_plane *primary;
118         int ret;
119
120         primary = kzalloc(sizeof(*primary), GFP_KERNEL);
121         if (primary == NULL) {
122                 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
123                 return NULL;
124         }
125
126         /*
127          * Remove the format_default field from drm_plane when dropping
128          * this helper.
129          */
130         primary->format_default = true;
131
132         /* possible_crtc's will be filled in later by crtc_init */
133         ret = drm_universal_plane_init(dev, primary, 0,
134                                        &drm_primary_helper_funcs,
135                                        safe_modeset_formats,
136                                        ARRAY_SIZE(safe_modeset_formats),
137                                        DRM_PLANE_TYPE_PRIMARY, NULL);
138         if (ret) {
139                 kfree(primary);
140                 primary = NULL;
141         }
142
143         return primary;
144 }
145
146 /**
147  * drm_crtc_init - Legacy CRTC initialization function
148  * @dev: DRM device
149  * @crtc: CRTC object to init
150  * @funcs: callbacks for the new CRTC
151  *
152  * Initialize a CRTC object with a default helper-provided primary plane and no
153  * cursor plane.
154  *
155  * Returns:
156  * Zero on success, error code on failure.
157  */
158 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
159                   const struct drm_crtc_funcs *funcs)
160 {
161         struct drm_plane *primary;
162
163         primary = create_primary_plane(dev);
164         return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs,
165                                          NULL);
166 }
167 EXPORT_SYMBOL(drm_crtc_init);