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 * @info: pixel format information 78 * @mode_cmd: metadata from the userspace fb creation request 79 * 80 * This helper can be used in a drivers fb_create callback to pre-fill the fb's 81 * metadata fields. 82 */ 83 void drm_helper_mode_fill_fb_struct(struct drm_device *dev, 84 struct drm_framebuffer *fb, 85 const struct drm_format_info *info, 86 const struct drm_mode_fb_cmd2 *mode_cmd) 87 { 88 int i; 89 90 fb->dev = dev; 91 fb->format = info; 92 fb->width = mode_cmd->width; 93 fb->height = mode_cmd->height; 94 for (i = 0; i < 4; i++) { 95 fb->pitches[i] = mode_cmd->pitches[i]; 96 fb->offsets[i] = mode_cmd->offsets[i]; 97 } 98 fb->modifier = mode_cmd->modifier[0]; 99 fb->flags = mode_cmd->flags; 100 } 101 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); 102 103 /* 104 * This is the minimal list of formats that seem to be safe for modeset use 105 * with all current DRM drivers. Most hardware can actually support more 106 * formats than this and drivers may specify a more accurate list when 107 * creating the primary plane. 108 */ 109 static const uint32_t safe_modeset_formats[] = { 110 DRM_FORMAT_XRGB8888, 111 DRM_FORMAT_ARGB8888, 112 }; 113 114 static const struct drm_plane_funcs primary_plane_funcs = { 115 DRM_PLANE_NON_ATOMIC_FUNCS, 116 }; 117 118 /** 119 * drm_crtc_init - Legacy CRTC initialization function 120 * @dev: DRM device 121 * @crtc: CRTC object to init 122 * @funcs: callbacks for the new CRTC 123 * 124 * Initialize a CRTC object with a default helper-provided primary plane and no 125 * cursor plane. 126 * 127 * Note that we make some assumptions about hardware limitations that may not be 128 * true for all hardware: 129 * 130 * 1. Primary plane cannot be repositioned. 131 * 2. Primary plane cannot be scaled. 132 * 3. Primary plane must cover the entire CRTC. 133 * 4. Subpixel positioning is not supported. 134 * 5. The primary plane must always be on if the CRTC is enabled. 135 * 136 * This is purely a backwards compatibility helper for old drivers. Drivers 137 * should instead implement their own primary plane. Atomic drivers must do so. 138 * 139 * Returns: 140 * Zero on success, error code on failure. 141 */ 142 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, 143 const struct drm_crtc_funcs *funcs) 144 { 145 struct drm_plane *primary; 146 int ret; 147 148 /* possible_crtc's will be filled in later by crtc_init */ 149 primary = __drm_universal_plane_alloc(dev, sizeof(*primary), 0, 0, 150 &primary_plane_funcs, 151 safe_modeset_formats, 152 ARRAY_SIZE(safe_modeset_formats), 153 NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 154 if (IS_ERR(primary)) 155 return PTR_ERR(primary); 156 157 /* 158 * Remove the format_default field from drm_plane when dropping 159 * this helper. 160 */ 161 primary->format_default = true; 162 163 ret = drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs, NULL); 164 if (ret) 165 goto err_drm_plane_cleanup; 166 167 return 0; 168 169 err_drm_plane_cleanup: 170 drm_plane_cleanup(primary); 171 kfree(primary); 172 return ret; 173 } 174 EXPORT_SYMBOL(drm_crtc_init); 175 176 /** 177 * drm_mode_config_helper_suspend - Modeset suspend helper 178 * @dev: DRM device 179 * 180 * This helper function takes care of suspending the modeset side. It disables 181 * output polling if initialized, suspends fbdev if used and finally calls 182 * drm_atomic_helper_suspend(). 183 * If suspending fails, fbdev and polling is re-enabled. 184 * 185 * Returns: 186 * Zero on success, negative error code on error. 187 * 188 * See also: 189 * drm_kms_helper_poll_disable() and drm_client_dev_suspend(). 190 */ 191 int drm_mode_config_helper_suspend(struct drm_device *dev) 192 { 193 struct drm_atomic_state *state; 194 195 if (!dev) 196 return 0; 197 /* 198 * Don't disable polling if it was never initialized 199 */ 200 if (dev->mode_config.poll_enabled) 201 drm_kms_helper_poll_disable(dev); 202 203 drm_client_dev_suspend(dev); 204 state = drm_atomic_helper_suspend(dev); 205 if (IS_ERR(state)) { 206 drm_client_dev_resume(dev); 207 208 /* 209 * Don't enable polling if it was never initialized 210 */ 211 if (dev->mode_config.poll_enabled) 212 drm_kms_helper_poll_enable(dev); 213 214 return PTR_ERR(state); 215 } 216 217 dev->mode_config.suspend_state = state; 218 219 return 0; 220 } 221 EXPORT_SYMBOL(drm_mode_config_helper_suspend); 222 223 /** 224 * drm_mode_config_helper_resume - Modeset resume helper 225 * @dev: DRM device 226 * 227 * This helper function takes care of resuming the modeset side. It calls 228 * drm_atomic_helper_resume(), resumes fbdev if used and enables output polling 229 * if initiaized. 230 * 231 * Returns: 232 * Zero on success, negative error code on error. 233 * 234 * See also: 235 * drm_client_dev_resume() and drm_kms_helper_poll_enable(). 236 */ 237 int drm_mode_config_helper_resume(struct drm_device *dev) 238 { 239 int ret; 240 241 if (!dev) 242 return 0; 243 244 if (WARN_ON(!dev->mode_config.suspend_state)) 245 return -EINVAL; 246 247 ret = drm_atomic_helper_resume(dev, dev->mode_config.suspend_state); 248 if (ret) 249 DRM_ERROR("Failed to resume (%d)\n", ret); 250 dev->mode_config.suspend_state = NULL; 251 252 drm_client_dev_resume(dev); 253 254 /* 255 * Don't enable polling if it is not initialized 256 */ 257 if (dev->mode_config.poll_enabled) 258 drm_kms_helper_poll_enable(dev); 259 260 return ret; 261 } 262 EXPORT_SYMBOL(drm_mode_config_helper_resume); 263