xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/drm_modeset_helper.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2016 Intel Corporation
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software and its
5*4882a593Smuzhiyun  * documentation for any purpose is hereby granted without fee, provided that
6*4882a593Smuzhiyun  * the above copyright notice appear in all copies and that both that copyright
7*4882a593Smuzhiyun  * notice and this permission notice appear in supporting documentation, and
8*4882a593Smuzhiyun  * that the name of the copyright holders not be used in advertising or
9*4882a593Smuzhiyun  * publicity pertaining to distribution of the software without specific,
10*4882a593Smuzhiyun  * written prior permission.  The copyright holders make no representations
11*4882a593Smuzhiyun  * about the suitability of this software for any purpose.  It is provided "as
12*4882a593Smuzhiyun  * is" without express or implied warranty.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*4882a593Smuzhiyun  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*4882a593Smuzhiyun  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*4882a593Smuzhiyun  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*4882a593Smuzhiyun  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*4882a593Smuzhiyun  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20*4882a593Smuzhiyun  * OF THIS SOFTWARE.
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <drm/drm_atomic_helper.h>
24*4882a593Smuzhiyun #include <drm/drm_fb_helper.h>
25*4882a593Smuzhiyun #include <drm/drm_fourcc.h>
26*4882a593Smuzhiyun #include <drm/drm_modeset_helper.h>
27*4882a593Smuzhiyun #include <drm/drm_plane_helper.h>
28*4882a593Smuzhiyun #include <drm/drm_print.h>
29*4882a593Smuzhiyun #include <drm/drm_probe_helper.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /**
32*4882a593Smuzhiyun  * DOC: aux kms helpers
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * This helper library contains various one-off functions which don't really fit
35*4882a593Smuzhiyun  * anywhere else in the DRM modeset helper library.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /**
39*4882a593Smuzhiyun  * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
40*4882a593Smuzhiyun  * 						connector list
41*4882a593Smuzhiyun  * @dev: drm device to operate on
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * Some userspace presumes that the first connected connector is the main
44*4882a593Smuzhiyun  * display, where it's supposed to display e.g. the login screen. For
45*4882a593Smuzhiyun  * laptops, this should be the main panel. Use this function to sort all
46*4882a593Smuzhiyun  * (eDP/LVDS/DSI) panels to the front of the connector list, instead of
47*4882a593Smuzhiyun  * painstakingly trying to initialize them in the right order.
48*4882a593Smuzhiyun  */
drm_helper_move_panel_connectors_to_head(struct drm_device * dev)49*4882a593Smuzhiyun void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	struct drm_connector *connector, *tmp;
52*4882a593Smuzhiyun 	struct list_head panel_list;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	INIT_LIST_HEAD(&panel_list);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	spin_lock_irq(&dev->mode_config.connector_list_lock);
57*4882a593Smuzhiyun 	list_for_each_entry_safe(connector, tmp,
58*4882a593Smuzhiyun 				 &dev->mode_config.connector_list, head) {
59*4882a593Smuzhiyun 		if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
60*4882a593Smuzhiyun 		    connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
61*4882a593Smuzhiyun 		    connector->connector_type == DRM_MODE_CONNECTOR_DSI)
62*4882a593Smuzhiyun 			list_move_tail(&connector->head, &panel_list);
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	list_splice(&panel_list, &dev->mode_config.connector_list);
66*4882a593Smuzhiyun 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun  * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
72*4882a593Smuzhiyun  * @dev: DRM device
73*4882a593Smuzhiyun  * @fb: drm_framebuffer object to fill out
74*4882a593Smuzhiyun  * @mode_cmd: metadata from the userspace fb creation request
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * This helper can be used in a drivers fb_create callback to pre-fill the fb's
77*4882a593Smuzhiyun  * metadata fields.
78*4882a593Smuzhiyun  */
drm_helper_mode_fill_fb_struct(struct drm_device * dev,struct drm_framebuffer * fb,const struct drm_mode_fb_cmd2 * mode_cmd)79*4882a593Smuzhiyun void drm_helper_mode_fill_fb_struct(struct drm_device *dev,
80*4882a593Smuzhiyun 				    struct drm_framebuffer *fb,
81*4882a593Smuzhiyun 				    const struct drm_mode_fb_cmd2 *mode_cmd)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	int i;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	fb->dev = dev;
86*4882a593Smuzhiyun 	fb->format = drm_get_format_info(dev, mode_cmd);
87*4882a593Smuzhiyun 	fb->width = mode_cmd->width;
88*4882a593Smuzhiyun 	fb->height = mode_cmd->height;
89*4882a593Smuzhiyun 	for (i = 0; i < 4; i++) {
90*4882a593Smuzhiyun 		fb->pitches[i] = mode_cmd->pitches[i];
91*4882a593Smuzhiyun 		fb->offsets[i] = mode_cmd->offsets[i];
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 	fb->modifier = mode_cmd->modifier[0];
94*4882a593Smuzhiyun 	fb->flags = mode_cmd->flags;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun  * This is the minimal list of formats that seem to be safe for modeset use
100*4882a593Smuzhiyun  * with all current DRM drivers.  Most hardware can actually support more
101*4882a593Smuzhiyun  * formats than this and drivers may specify a more accurate list when
102*4882a593Smuzhiyun  * creating the primary plane.  However drivers that still call
103*4882a593Smuzhiyun  * drm_plane_init() will use this minimal format list as the default.
104*4882a593Smuzhiyun  */
105*4882a593Smuzhiyun static const uint32_t safe_modeset_formats[] = {
106*4882a593Smuzhiyun 	DRM_FORMAT_XRGB8888,
107*4882a593Smuzhiyun 	DRM_FORMAT_ARGB8888,
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun 
create_primary_plane(struct drm_device * dev)110*4882a593Smuzhiyun static struct drm_plane *create_primary_plane(struct drm_device *dev)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	struct drm_plane *primary;
113*4882a593Smuzhiyun 	int ret;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
116*4882a593Smuzhiyun 	if (primary == NULL) {
117*4882a593Smuzhiyun 		DRM_DEBUG_KMS("Failed to allocate primary plane\n");
118*4882a593Smuzhiyun 		return NULL;
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/*
122*4882a593Smuzhiyun 	 * Remove the format_default field from drm_plane when dropping
123*4882a593Smuzhiyun 	 * this helper.
124*4882a593Smuzhiyun 	 */
125*4882a593Smuzhiyun 	primary->format_default = true;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/* possible_crtc's will be filled in later by crtc_init */
128*4882a593Smuzhiyun 	ret = drm_universal_plane_init(dev, primary, 0,
129*4882a593Smuzhiyun 				       &drm_primary_helper_funcs,
130*4882a593Smuzhiyun 				       safe_modeset_formats,
131*4882a593Smuzhiyun 				       ARRAY_SIZE(safe_modeset_formats),
132*4882a593Smuzhiyun 				       NULL,
133*4882a593Smuzhiyun 				       DRM_PLANE_TYPE_PRIMARY, NULL);
134*4882a593Smuzhiyun 	if (ret) {
135*4882a593Smuzhiyun 		kfree(primary);
136*4882a593Smuzhiyun 		primary = NULL;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return primary;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun  * drm_crtc_init - Legacy CRTC initialization function
144*4882a593Smuzhiyun  * @dev: DRM device
145*4882a593Smuzhiyun  * @crtc: CRTC object to init
146*4882a593Smuzhiyun  * @funcs: callbacks for the new CRTC
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * Initialize a CRTC object with a default helper-provided primary plane and no
149*4882a593Smuzhiyun  * cursor plane.
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  * Note that we make some assumptions about hardware limitations that may not be
152*4882a593Smuzhiyun  * true for all hardware:
153*4882a593Smuzhiyun  *
154*4882a593Smuzhiyun  * 1. Primary plane cannot be repositioned.
155*4882a593Smuzhiyun  * 2. Primary plane cannot be scaled.
156*4882a593Smuzhiyun  * 3. Primary plane must cover the entire CRTC.
157*4882a593Smuzhiyun  * 4. Subpixel positioning is not supported.
158*4882a593Smuzhiyun  * 5. The primary plane must always be on if the CRTC is enabled.
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun  * This is purely a backwards compatibility helper for old drivers. Drivers
161*4882a593Smuzhiyun  * should instead implement their own primary plane. Atomic drivers must do so.
162*4882a593Smuzhiyun  * Drivers with the above hardware restriction can look into using &struct
163*4882a593Smuzhiyun  * drm_simple_display_pipe, which encapsulates the above limitations into a nice
164*4882a593Smuzhiyun  * interface.
165*4882a593Smuzhiyun  *
166*4882a593Smuzhiyun  * Returns:
167*4882a593Smuzhiyun  * Zero on success, error code on failure.
168*4882a593Smuzhiyun  */
drm_crtc_init(struct drm_device * dev,struct drm_crtc * crtc,const struct drm_crtc_funcs * funcs)169*4882a593Smuzhiyun int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
170*4882a593Smuzhiyun 		  const struct drm_crtc_funcs *funcs)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	struct drm_plane *primary;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	primary = create_primary_plane(dev);
175*4882a593Smuzhiyun 	return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs,
176*4882a593Smuzhiyun 					 NULL);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun EXPORT_SYMBOL(drm_crtc_init);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /**
181*4882a593Smuzhiyun  * drm_mode_config_helper_suspend - Modeset suspend helper
182*4882a593Smuzhiyun  * @dev: DRM device
183*4882a593Smuzhiyun  *
184*4882a593Smuzhiyun  * This helper function takes care of suspending the modeset side. It disables
185*4882a593Smuzhiyun  * output polling if initialized, suspends fbdev if used and finally calls
186*4882a593Smuzhiyun  * drm_atomic_helper_suspend().
187*4882a593Smuzhiyun  * If suspending fails, fbdev and polling is re-enabled.
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * Returns:
190*4882a593Smuzhiyun  * Zero on success, negative error code on error.
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * See also:
193*4882a593Smuzhiyun  * drm_kms_helper_poll_disable() and drm_fb_helper_set_suspend_unlocked().
194*4882a593Smuzhiyun  */
drm_mode_config_helper_suspend(struct drm_device * dev)195*4882a593Smuzhiyun int drm_mode_config_helper_suspend(struct drm_device *dev)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct drm_atomic_state *state;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	if (!dev)
200*4882a593Smuzhiyun 		return 0;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	drm_kms_helper_poll_disable(dev);
203*4882a593Smuzhiyun 	drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 1);
204*4882a593Smuzhiyun 	state = drm_atomic_helper_suspend(dev);
205*4882a593Smuzhiyun 	if (IS_ERR(state)) {
206*4882a593Smuzhiyun 		drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
207*4882a593Smuzhiyun 		drm_kms_helper_poll_enable(dev);
208*4882a593Smuzhiyun 		return PTR_ERR(state);
209*4882a593Smuzhiyun 	}
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	dev->mode_config.suspend_state = state;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	return 0;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun EXPORT_SYMBOL(drm_mode_config_helper_suspend);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /**
218*4882a593Smuzhiyun  * drm_mode_config_helper_resume - Modeset resume helper
219*4882a593Smuzhiyun  * @dev: DRM device
220*4882a593Smuzhiyun  *
221*4882a593Smuzhiyun  * This helper function takes care of resuming the modeset side. It calls
222*4882a593Smuzhiyun  * drm_atomic_helper_resume(), resumes fbdev if used and enables output polling
223*4882a593Smuzhiyun  * if initiaized.
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Returns:
226*4882a593Smuzhiyun  * Zero on success, negative error code on error.
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  * See also:
229*4882a593Smuzhiyun  * drm_fb_helper_set_suspend_unlocked() and drm_kms_helper_poll_enable().
230*4882a593Smuzhiyun  */
drm_mode_config_helper_resume(struct drm_device * dev)231*4882a593Smuzhiyun int drm_mode_config_helper_resume(struct drm_device *dev)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	int ret;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (!dev)
236*4882a593Smuzhiyun 		return 0;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	if (WARN_ON(!dev->mode_config.suspend_state))
239*4882a593Smuzhiyun 		return -EINVAL;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	ret = drm_atomic_helper_resume(dev, dev->mode_config.suspend_state);
242*4882a593Smuzhiyun 	if (ret)
243*4882a593Smuzhiyun 		DRM_ERROR("Failed to resume (%d)\n", ret);
244*4882a593Smuzhiyun 	dev->mode_config.suspend_state = NULL;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
247*4882a593Smuzhiyun 	drm_kms_helper_poll_enable(dev);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	return ret;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun EXPORT_SYMBOL(drm_mode_config_helper_resume);
252