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