xref: /linux/drivers/gpu/drm/drm_modeset_helper.c (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
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 <linux/export.h>
24 
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_client_event.h>
27 #include <drm/drm_fourcc.h>
28 #include <drm/drm_framebuffer.h>
29 #include <drm/drm_modeset_helper.h>
30 #include <drm/drm_panic_helper.h>
31 #include <drm/drm_plane_helper.h>
32 #include <drm/drm_print.h>
33 #include <drm/drm_probe_helper.h>
34 
35 /**
36  * DOC: aux kms helpers
37  *
38  * This helper library contains various one-off functions which don't really fit
39  * anywhere else in the DRM modeset helper library.
40  */
41 
42 /**
43  * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
44  * 						connector list
45  * @dev: drm device to operate on
46  *
47  * Some userspace presumes that the first connected connector is the main
48  * display, where it's supposed to display e.g. the login screen. For
49  * laptops, this should be the main panel. Use this function to sort all
50  * (eDP/LVDS/DSI) panels to the front of the connector list, instead of
51  * painstakingly trying to initialize them in the right order.
52  */
53 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
54 {
55 	struct drm_connector *connector, *tmp;
56 	struct list_head panel_list;
57 
58 	INIT_LIST_HEAD(&panel_list);
59 
60 	spin_lock_irq(&dev->mode_config.connector_list_lock);
61 	list_for_each_entry_safe(connector, tmp,
62 				 &dev->mode_config.connector_list, head) {
63 		if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
64 		    connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
65 		    connector->connector_type == DRM_MODE_CONNECTOR_DSI)
66 			list_move_tail(&connector->head, &panel_list);
67 	}
68 
69 	list_splice(&panel_list, &dev->mode_config.connector_list);
70 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
71 }
72 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
73 
74 /**
75  * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
76  * @dev: DRM device
77  * @fb: drm_framebuffer object to fill out
78  * @info: pixel format information
79  * @mode_cmd: metadata from the userspace fb creation request
80  *
81  * This helper can be used in a drivers fb_create callback to pre-fill the fb's
82  * metadata fields.
83  */
84 void drm_helper_mode_fill_fb_struct(struct drm_device *dev,
85 				    struct drm_framebuffer *fb,
86 				    const struct drm_format_info *info,
87 				    const struct drm_mode_fb_cmd2 *mode_cmd)
88 {
89 	int i;
90 
91 	fb->dev = dev;
92 	fb->format = info;
93 	fb->width = mode_cmd->width;
94 	fb->height = mode_cmd->height;
95 	for (i = 0; i < 4; i++) {
96 		fb->pitches[i] = mode_cmd->pitches[i];
97 		fb->offsets[i] = mode_cmd->offsets[i];
98 	}
99 	fb->modifier = mode_cmd->modifier[0];
100 	fb->flags = mode_cmd->flags;
101 }
102 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
103 
104 /*
105  * This is the minimal list of formats that seem to be safe for modeset use
106  * with all current DRM drivers.  Most hardware can actually support more
107  * formats than this and drivers may specify a more accurate list when
108  * creating the primary plane.
109  */
110 static const uint32_t safe_modeset_formats[] = {
111 	DRM_FORMAT_XRGB8888,
112 	DRM_FORMAT_ARGB8888,
113 };
114 
115 static const struct drm_plane_funcs primary_plane_funcs = {
116 	DRM_PLANE_NON_ATOMIC_FUNCS,
117 	DRM_PANIC_PLANE_FUNCS,
118 };
119 
120 /**
121  * drm_crtc_init - Legacy CRTC initialization function
122  * @dev: DRM device
123  * @crtc: CRTC object to init
124  * @funcs: callbacks for the new CRTC
125  *
126  * Initialize a CRTC object with a default helper-provided primary plane and no
127  * cursor plane.
128  *
129  * Note that we make some assumptions about hardware limitations that may not be
130  * true for all hardware:
131  *
132  * 1. Primary plane cannot be repositioned.
133  * 2. Primary plane cannot be scaled.
134  * 3. Primary plane must cover the entire CRTC.
135  * 4. Subpixel positioning is not supported.
136  * 5. The primary plane must always be on if the CRTC is enabled.
137  *
138  * This is purely a backwards compatibility helper for old drivers. Drivers
139  * should instead implement their own primary plane. Atomic drivers must do so.
140  *
141  * Returns:
142  * Zero on success, error code on failure.
143  */
144 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
145 		  const struct drm_crtc_funcs *funcs)
146 {
147 	struct drm_plane *primary;
148 	int ret;
149 
150 	/* possible_crtc's will be filled in later by crtc_init */
151 	primary = __drm_universal_plane_alloc(dev, sizeof(*primary), 0, 0,
152 					      &primary_plane_funcs,
153 					      safe_modeset_formats,
154 					      ARRAY_SIZE(safe_modeset_formats),
155 					      NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
156 	if (IS_ERR(primary))
157 		return PTR_ERR(primary);
158 
159 	/*
160 	 * Remove the format_default field from drm_plane when dropping
161 	 * this helper.
162 	 */
163 	primary->format_default = true;
164 
165 	ret = drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs, NULL);
166 	if (ret)
167 		goto err_drm_plane_cleanup;
168 
169 	return 0;
170 
171 err_drm_plane_cleanup:
172 	drm_plane_cleanup(primary);
173 	kfree(primary);
174 	return ret;
175 }
176 EXPORT_SYMBOL(drm_crtc_init);
177 
178 /**
179  * drm_mode_config_helper_suspend - Modeset suspend helper
180  * @dev: DRM device
181  *
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.
186  *
187  * Returns:
188  * Zero on success, negative error code on error.
189  *
190  * See also:
191  * drm_kms_helper_poll_disable() and drm_client_dev_suspend().
192  */
193 int drm_mode_config_helper_suspend(struct drm_device *dev)
194 {
195 	struct drm_atomic_commit *state;
196 
197 	if (!dev)
198 		return 0;
199 	/*
200 	 * Don't disable polling if it was never initialized
201 	 */
202 	if (dev->mode_config.poll_enabled)
203 		drm_kms_helper_poll_disable(dev);
204 
205 	drm_client_dev_suspend(dev);
206 	state = drm_atomic_helper_suspend(dev);
207 	if (IS_ERR(state)) {
208 		drm_client_dev_resume(dev);
209 
210 		/*
211 		 * Don't enable polling if it was never initialized
212 		 */
213 		if (dev->mode_config.poll_enabled)
214 			drm_kms_helper_poll_enable(dev);
215 
216 		return PTR_ERR(state);
217 	}
218 
219 	dev->mode_config.suspend_state = state;
220 
221 	return 0;
222 }
223 EXPORT_SYMBOL(drm_mode_config_helper_suspend);
224 
225 /**
226  * drm_mode_config_helper_resume - Modeset resume helper
227  * @dev: DRM device
228  *
229  * This helper function takes care of resuming the modeset side. It calls
230  * drm_atomic_helper_resume(), resumes fbdev if used and enables output polling
231  * if initiaized.
232  *
233  * Returns:
234  * Zero on success, negative error code on error.
235  *
236  * See also:
237  * drm_client_dev_resume() and drm_kms_helper_poll_enable().
238  */
239 int drm_mode_config_helper_resume(struct drm_device *dev)
240 {
241 	int ret;
242 
243 	if (!dev)
244 		return 0;
245 
246 	if (WARN_ON(!dev->mode_config.suspend_state))
247 		return -EINVAL;
248 
249 	ret = drm_atomic_helper_resume(dev, dev->mode_config.suspend_state);
250 	if (ret)
251 		DRM_ERROR("Failed to resume (%d)\n", ret);
252 	dev->mode_config.suspend_state = NULL;
253 
254 	drm_client_dev_resume(dev);
255 
256 	/*
257 	 * Don't enable polling if it is not initialized
258 	 */
259 	if (dev->mode_config.poll_enabled)
260 		drm_kms_helper_poll_enable(dev);
261 
262 	return ret;
263 }
264 EXPORT_SYMBOL(drm_mode_config_helper_resume);
265