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); 90 fb->width = mode_cmd->width; 91 fb->height = mode_cmd->height; 92 for (i = 0; i < 4; i++) { 93 fb->pitches[i] = mode_cmd->pitches[i]; 94 fb->offsets[i] = mode_cmd->offsets[i]; 95 } 96 fb->modifier = mode_cmd->modifier[0]; 97 fb->flags = mode_cmd->flags; 98 } 99 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); 100 101 /* 102 * This is the minimal list of formats that seem to be safe for modeset use 103 * with all current DRM drivers. Most hardware can actually support more 104 * formats than this and drivers may specify a more accurate list when 105 * creating the primary plane. 106 */ 107 static const uint32_t safe_modeset_formats[] = { 108 DRM_FORMAT_XRGB8888, 109 DRM_FORMAT_ARGB8888, 110 }; 111 112 static const struct drm_plane_funcs primary_plane_funcs = { 113 DRM_PLANE_NON_ATOMIC_FUNCS, 114 }; 115 116 /** 117 * drm_crtc_init - Legacy CRTC initialization function 118 * @dev: DRM device 119 * @crtc: CRTC object to init 120 * @funcs: callbacks for the new CRTC 121 * 122 * Initialize a CRTC object with a default helper-provided primary plane and no 123 * cursor plane. 124 * 125 * Note that we make some assumptions about hardware limitations that may not be 126 * true for all hardware: 127 * 128 * 1. Primary plane cannot be repositioned. 129 * 2. Primary plane cannot be scaled. 130 * 3. Primary plane must cover the entire CRTC. 131 * 4. Subpixel positioning is not supported. 132 * 5. The primary plane must always be on if the CRTC is enabled. 133 * 134 * This is purely a backwards compatibility helper for old drivers. Drivers 135 * should instead implement their own primary plane. Atomic drivers must do so. 136 * Drivers with the above hardware restriction can look into using &struct 137 * drm_simple_display_pipe, which encapsulates the above limitations into a nice 138 * interface. 139 * 140 * Returns: 141 * Zero on success, error code on failure. 142 */ 143 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, 144 const struct drm_crtc_funcs *funcs) 145 { 146 struct drm_plane *primary; 147 int ret; 148 149 /* possible_crtc's will be filled in later by crtc_init */ 150 primary = __drm_universal_plane_alloc(dev, sizeof(*primary), 0, 0, 151 &primary_plane_funcs, 152 safe_modeset_formats, 153 ARRAY_SIZE(safe_modeset_formats), 154 NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 155 if (IS_ERR(primary)) 156 return PTR_ERR(primary); 157 158 /* 159 * Remove the format_default field from drm_plane when dropping 160 * this helper. 161 */ 162 primary->format_default = true; 163 164 ret = drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs, NULL); 165 if (ret) 166 goto err_drm_plane_cleanup; 167 168 return 0; 169 170 err_drm_plane_cleanup: 171 drm_plane_cleanup(primary); 172 kfree(primary); 173 return ret; 174 } 175 EXPORT_SYMBOL(drm_crtc_init); 176 177 /** 178 * drm_mode_config_helper_suspend - Modeset suspend helper 179 * @dev: DRM device 180 * 181 * This helper function takes care of suspending the modeset side. It disables 182 * output polling if initialized, suspends fbdev if used and finally calls 183 * drm_atomic_helper_suspend(). 184 * If suspending fails, fbdev and polling is re-enabled. 185 * 186 * Returns: 187 * Zero on success, negative error code on error. 188 * 189 * See also: 190 * drm_kms_helper_poll_disable() and drm_client_dev_suspend(). 191 */ 192 int drm_mode_config_helper_suspend(struct drm_device *dev) 193 { 194 struct drm_atomic_state *state; 195 196 if (!dev) 197 return 0; 198 /* 199 * Don't disable polling if it was never initialized 200 */ 201 if (dev->mode_config.poll_enabled) 202 drm_kms_helper_poll_disable(dev); 203 204 drm_client_dev_suspend(dev, false); 205 state = drm_atomic_helper_suspend(dev); 206 if (IS_ERR(state)) { 207 drm_client_dev_resume(dev, false); 208 209 /* 210 * Don't enable polling if it was never initialized 211 */ 212 if (dev->mode_config.poll_enabled) 213 drm_kms_helper_poll_enable(dev); 214 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_client_dev_resume() 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_client_dev_resume(dev, false); 254 255 /* 256 * Don't enable polling if it is not initialized 257 */ 258 if (dev->mode_config.poll_enabled) 259 drm_kms_helper_poll_enable(dev); 260 261 return ret; 262 } 263 EXPORT_SYMBOL(drm_mode_config_helper_resume); 264