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_plane_helper.h> 31 #include <drm/drm_print.h> 32 #include <drm/drm_probe_helper.h> 33 34 /** 35 * DOC: aux kms helpers 36 * 37 * This helper library contains various one-off functions which don't really fit 38 * anywhere else in the DRM modeset helper library. 39 */ 40 41 /** 42 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the 43 * connector list 44 * @dev: drm device to operate on 45 * 46 * Some userspace presumes that the first connected connector is the main 47 * display, where it's supposed to display e.g. the login screen. For 48 * laptops, this should be the main panel. Use this function to sort all 49 * (eDP/LVDS/DSI) panels to the front of the connector list, instead of 50 * painstakingly trying to initialize them in the right order. 51 */ 52 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev) 53 { 54 struct drm_connector *connector, *tmp; 55 struct list_head panel_list; 56 57 INIT_LIST_HEAD(&panel_list); 58 59 spin_lock_irq(&dev->mode_config.connector_list_lock); 60 list_for_each_entry_safe(connector, tmp, 61 &dev->mode_config.connector_list, head) { 62 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS || 63 connector->connector_type == DRM_MODE_CONNECTOR_eDP || 64 connector->connector_type == DRM_MODE_CONNECTOR_DSI) 65 list_move_tail(&connector->head, &panel_list); 66 } 67 68 list_splice(&panel_list, &dev->mode_config.connector_list); 69 spin_unlock_irq(&dev->mode_config.connector_list_lock); 70 } 71 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head); 72 73 /** 74 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata 75 * @dev: DRM device 76 * @fb: drm_framebuffer object to fill out 77 * @mode_cmd: metadata from the userspace fb creation request 78 * 79 * This helper can be used in a drivers fb_create callback to pre-fill the fb's 80 * metadata fields. 81 */ 82 void drm_helper_mode_fill_fb_struct(struct drm_device *dev, 83 struct drm_framebuffer *fb, 84 const struct drm_mode_fb_cmd2 *mode_cmd) 85 { 86 int i; 87 88 fb->dev = dev; 89 fb->format = drm_get_format_info(dev, mode_cmd->pixel_format, 90 mode_cmd->modifier[0]); 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 } 97 fb->modifier = mode_cmd->modifier[0]; 98 fb->flags = mode_cmd->flags; 99 } 100 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); 101 102 /* 103 * This is the minimal list of formats that seem to be safe for modeset use 104 * with all current DRM drivers. Most hardware can actually support more 105 * formats than this and drivers may specify a more accurate list when 106 * creating the primary plane. 107 */ 108 static const uint32_t safe_modeset_formats[] = { 109 DRM_FORMAT_XRGB8888, 110 DRM_FORMAT_ARGB8888, 111 }; 112 113 static const struct drm_plane_funcs primary_plane_funcs = { 114 DRM_PLANE_NON_ATOMIC_FUNCS, 115 }; 116 117 /** 118 * drm_crtc_init - Legacy CRTC initialization function 119 * @dev: DRM device 120 * @crtc: CRTC object to init 121 * @funcs: callbacks for the new CRTC 122 * 123 * Initialize a CRTC object with a default helper-provided primary plane and no 124 * cursor plane. 125 * 126 * Note that we make some assumptions about hardware limitations that may not be 127 * true for all hardware: 128 * 129 * 1. Primary plane cannot be repositioned. 130 * 2. Primary plane cannot be scaled. 131 * 3. Primary plane must cover the entire CRTC. 132 * 4. Subpixel positioning is not supported. 133 * 5. The primary plane must always be on if the CRTC is enabled. 134 * 135 * This is purely a backwards compatibility helper for old drivers. Drivers 136 * should instead implement their own primary plane. Atomic drivers must do so. 137 * Drivers with the above hardware restriction can look into using &struct 138 * drm_simple_display_pipe, which encapsulates the above limitations into a nice 139 * interface. 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_state *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, false); 206 state = drm_atomic_helper_suspend(dev); 207 if (IS_ERR(state)) { 208 drm_client_dev_resume(dev, false); 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, false); 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