18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2016 Intel Corporation
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software and its
58c2ecf20Sopenharmony_ci * documentation for any purpose is hereby granted without fee, provided that
68c2ecf20Sopenharmony_ci * the above copyright notice appear in all copies and that both that copyright
78c2ecf20Sopenharmony_ci * notice and this permission notice appear in supporting documentation, and
88c2ecf20Sopenharmony_ci * that the name of the copyright holders not be used in advertising or
98c2ecf20Sopenharmony_ci * publicity pertaining to distribution of the software without specific,
108c2ecf20Sopenharmony_ci * written prior permission.  The copyright holders make no representations
118c2ecf20Sopenharmony_ci * about the suitability of this software for any purpose.  It is provided "as
128c2ecf20Sopenharmony_ci * is" without express or implied warranty.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
158c2ecf20Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
168c2ecf20Sopenharmony_ci * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
178c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
188c2ecf20Sopenharmony_ci * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
198c2ecf20Sopenharmony_ci * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
208c2ecf20Sopenharmony_ci * OF THIS SOFTWARE.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h>
248c2ecf20Sopenharmony_ci#include <drm/drm_fb_helper.h>
258c2ecf20Sopenharmony_ci#include <drm/drm_fourcc.h>
268c2ecf20Sopenharmony_ci#include <drm/drm_modeset_helper.h>
278c2ecf20Sopenharmony_ci#include <drm/drm_plane_helper.h>
288c2ecf20Sopenharmony_ci#include <drm/drm_print.h>
298c2ecf20Sopenharmony_ci#include <drm/drm_probe_helper.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/**
328c2ecf20Sopenharmony_ci * DOC: aux kms helpers
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * This helper library contains various one-off functions which don't really fit
358c2ecf20Sopenharmony_ci * anywhere else in the DRM modeset helper library.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/**
398c2ecf20Sopenharmony_ci * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
408c2ecf20Sopenharmony_ci * 						connector list
418c2ecf20Sopenharmony_ci * @dev: drm device to operate on
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * Some userspace presumes that the first connected connector is the main
448c2ecf20Sopenharmony_ci * display, where it's supposed to display e.g. the login screen. For
458c2ecf20Sopenharmony_ci * laptops, this should be the main panel. Use this function to sort all
468c2ecf20Sopenharmony_ci * (eDP/LVDS/DSI) panels to the front of the connector list, instead of
478c2ecf20Sopenharmony_ci * painstakingly trying to initialize them in the right order.
488c2ecf20Sopenharmony_ci */
498c2ecf20Sopenharmony_civoid drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct drm_connector *connector, *tmp;
528c2ecf20Sopenharmony_ci	struct list_head panel_list;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&panel_list);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	spin_lock_irq(&dev->mode_config.connector_list_lock);
578c2ecf20Sopenharmony_ci	list_for_each_entry_safe(connector, tmp,
588c2ecf20Sopenharmony_ci				 &dev->mode_config.connector_list, head) {
598c2ecf20Sopenharmony_ci		if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
608c2ecf20Sopenharmony_ci		    connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
618c2ecf20Sopenharmony_ci		    connector->connector_type == DRM_MODE_CONNECTOR_DSI)
628c2ecf20Sopenharmony_ci			list_move_tail(&connector->head, &panel_list);
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	list_splice(&panel_list, &dev->mode_config.connector_list);
668c2ecf20Sopenharmony_ci	spin_unlock_irq(&dev->mode_config.connector_list_lock);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/**
718c2ecf20Sopenharmony_ci * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
728c2ecf20Sopenharmony_ci * @dev: DRM device
738c2ecf20Sopenharmony_ci * @fb: drm_framebuffer object to fill out
748c2ecf20Sopenharmony_ci * @mode_cmd: metadata from the userspace fb creation request
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * This helper can be used in a drivers fb_create callback to pre-fill the fb's
778c2ecf20Sopenharmony_ci * metadata fields.
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_civoid drm_helper_mode_fill_fb_struct(struct drm_device *dev,
808c2ecf20Sopenharmony_ci				    struct drm_framebuffer *fb,
818c2ecf20Sopenharmony_ci				    const struct drm_mode_fb_cmd2 *mode_cmd)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	int i;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	fb->dev = dev;
868c2ecf20Sopenharmony_ci	fb->format = drm_get_format_info(dev, mode_cmd);
878c2ecf20Sopenharmony_ci	fb->width = mode_cmd->width;
888c2ecf20Sopenharmony_ci	fb->height = mode_cmd->height;
898c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
908c2ecf20Sopenharmony_ci		fb->pitches[i] = mode_cmd->pitches[i];
918c2ecf20Sopenharmony_ci		fb->offsets[i] = mode_cmd->offsets[i];
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci	fb->modifier = mode_cmd->modifier[0];
948c2ecf20Sopenharmony_ci	fb->flags = mode_cmd->flags;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/*
998c2ecf20Sopenharmony_ci * This is the minimal list of formats that seem to be safe for modeset use
1008c2ecf20Sopenharmony_ci * with all current DRM drivers.  Most hardware can actually support more
1018c2ecf20Sopenharmony_ci * formats than this and drivers may specify a more accurate list when
1028c2ecf20Sopenharmony_ci * creating the primary plane.  However drivers that still call
1038c2ecf20Sopenharmony_ci * drm_plane_init() will use this minimal format list as the default.
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_cistatic const uint32_t safe_modeset_formats[] = {
1068c2ecf20Sopenharmony_ci	DRM_FORMAT_XRGB8888,
1078c2ecf20Sopenharmony_ci	DRM_FORMAT_ARGB8888,
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic struct drm_plane *create_primary_plane(struct drm_device *dev)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	struct drm_plane *primary;
1138c2ecf20Sopenharmony_ci	int ret;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
1168c2ecf20Sopenharmony_ci	if (primary == NULL) {
1178c2ecf20Sopenharmony_ci		DRM_DEBUG_KMS("Failed to allocate primary plane\n");
1188c2ecf20Sopenharmony_ci		return NULL;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/*
1228c2ecf20Sopenharmony_ci	 * Remove the format_default field from drm_plane when dropping
1238c2ecf20Sopenharmony_ci	 * this helper.
1248c2ecf20Sopenharmony_ci	 */
1258c2ecf20Sopenharmony_ci	primary->format_default = true;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/* possible_crtc's will be filled in later by crtc_init */
1288c2ecf20Sopenharmony_ci	ret = drm_universal_plane_init(dev, primary, 0,
1298c2ecf20Sopenharmony_ci				       &drm_primary_helper_funcs,
1308c2ecf20Sopenharmony_ci				       safe_modeset_formats,
1318c2ecf20Sopenharmony_ci				       ARRAY_SIZE(safe_modeset_formats),
1328c2ecf20Sopenharmony_ci				       NULL,
1338c2ecf20Sopenharmony_ci				       DRM_PLANE_TYPE_PRIMARY, NULL);
1348c2ecf20Sopenharmony_ci	if (ret) {
1358c2ecf20Sopenharmony_ci		kfree(primary);
1368c2ecf20Sopenharmony_ci		primary = NULL;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return primary;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * drm_crtc_init - Legacy CRTC initialization function
1448c2ecf20Sopenharmony_ci * @dev: DRM device
1458c2ecf20Sopenharmony_ci * @crtc: CRTC object to init
1468c2ecf20Sopenharmony_ci * @funcs: callbacks for the new CRTC
1478c2ecf20Sopenharmony_ci *
1488c2ecf20Sopenharmony_ci * Initialize a CRTC object with a default helper-provided primary plane and no
1498c2ecf20Sopenharmony_ci * cursor plane.
1508c2ecf20Sopenharmony_ci *
1518c2ecf20Sopenharmony_ci * Note that we make some assumptions about hardware limitations that may not be
1528c2ecf20Sopenharmony_ci * true for all hardware:
1538c2ecf20Sopenharmony_ci *
1548c2ecf20Sopenharmony_ci * 1. Primary plane cannot be repositioned.
1558c2ecf20Sopenharmony_ci * 2. Primary plane cannot be scaled.
1568c2ecf20Sopenharmony_ci * 3. Primary plane must cover the entire CRTC.
1578c2ecf20Sopenharmony_ci * 4. Subpixel positioning is not supported.
1588c2ecf20Sopenharmony_ci * 5. The primary plane must always be on if the CRTC is enabled.
1598c2ecf20Sopenharmony_ci *
1608c2ecf20Sopenharmony_ci * This is purely a backwards compatibility helper for old drivers. Drivers
1618c2ecf20Sopenharmony_ci * should instead implement their own primary plane. Atomic drivers must do so.
1628c2ecf20Sopenharmony_ci * Drivers with the above hardware restriction can look into using &struct
1638c2ecf20Sopenharmony_ci * drm_simple_display_pipe, which encapsulates the above limitations into a nice
1648c2ecf20Sopenharmony_ci * interface.
1658c2ecf20Sopenharmony_ci *
1668c2ecf20Sopenharmony_ci * Returns:
1678c2ecf20Sopenharmony_ci * Zero on success, error code on failure.
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_ciint drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
1708c2ecf20Sopenharmony_ci		  const struct drm_crtc_funcs *funcs)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct drm_plane *primary;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	primary = create_primary_plane(dev);
1758c2ecf20Sopenharmony_ci	return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs,
1768c2ecf20Sopenharmony_ci					 NULL);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_crtc_init);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/**
1818c2ecf20Sopenharmony_ci * drm_mode_config_helper_suspend - Modeset suspend helper
1828c2ecf20Sopenharmony_ci * @dev: DRM device
1838c2ecf20Sopenharmony_ci *
1848c2ecf20Sopenharmony_ci * This helper function takes care of suspending the modeset side. It disables
1858c2ecf20Sopenharmony_ci * output polling if initialized, suspends fbdev if used and finally calls
1868c2ecf20Sopenharmony_ci * drm_atomic_helper_suspend().
1878c2ecf20Sopenharmony_ci * If suspending fails, fbdev and polling is re-enabled.
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * Returns:
1908c2ecf20Sopenharmony_ci * Zero on success, negative error code on error.
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci * See also:
1938c2ecf20Sopenharmony_ci * drm_kms_helper_poll_disable() and drm_fb_helper_set_suspend_unlocked().
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_ciint drm_mode_config_helper_suspend(struct drm_device *dev)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct drm_atomic_state *state;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	if (!dev)
2008c2ecf20Sopenharmony_ci		return 0;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	drm_kms_helper_poll_disable(dev);
2038c2ecf20Sopenharmony_ci	drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 1);
2048c2ecf20Sopenharmony_ci	state = drm_atomic_helper_suspend(dev);
2058c2ecf20Sopenharmony_ci	if (IS_ERR(state)) {
2068c2ecf20Sopenharmony_ci		drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
2078c2ecf20Sopenharmony_ci		drm_kms_helper_poll_enable(dev);
2088c2ecf20Sopenharmony_ci		return PTR_ERR(state);
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	dev->mode_config.suspend_state = state;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	return 0;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_mode_config_helper_suspend);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/**
2188c2ecf20Sopenharmony_ci * drm_mode_config_helper_resume - Modeset resume helper
2198c2ecf20Sopenharmony_ci * @dev: DRM device
2208c2ecf20Sopenharmony_ci *
2218c2ecf20Sopenharmony_ci * This helper function takes care of resuming the modeset side. It calls
2228c2ecf20Sopenharmony_ci * drm_atomic_helper_resume(), resumes fbdev if used and enables output polling
2238c2ecf20Sopenharmony_ci * if initiaized.
2248c2ecf20Sopenharmony_ci *
2258c2ecf20Sopenharmony_ci * Returns:
2268c2ecf20Sopenharmony_ci * Zero on success, negative error code on error.
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci * See also:
2298c2ecf20Sopenharmony_ci * drm_fb_helper_set_suspend_unlocked() and drm_kms_helper_poll_enable().
2308c2ecf20Sopenharmony_ci */
2318c2ecf20Sopenharmony_ciint drm_mode_config_helper_resume(struct drm_device *dev)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	int ret;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (!dev)
2368c2ecf20Sopenharmony_ci		return 0;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	if (WARN_ON(!dev->mode_config.suspend_state))
2398c2ecf20Sopenharmony_ci		return -EINVAL;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	ret = drm_atomic_helper_resume(dev, dev->mode_config.suspend_state);
2428c2ecf20Sopenharmony_ci	if (ret)
2438c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to resume (%d)\n", ret);
2448c2ecf20Sopenharmony_ci	dev->mode_config.suspend_state = NULL;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	drm_fb_helper_set_suspend_unlocked(dev->fb_helper, 0);
2478c2ecf20Sopenharmony_ci	drm_kms_helper_poll_enable(dev);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return ret;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ciEXPORT_SYMBOL(drm_mode_config_helper_resume);
252