xref: /linux/drivers/gpu/drm/drm_connector.c (revision b792e64021ecdefd27b84eb235d59b0476d4255c)
152217195SDaniel Vetter /*
252217195SDaniel Vetter  * Copyright (c) 2016 Intel Corporation
352217195SDaniel Vetter  *
452217195SDaniel Vetter  * Permission to use, copy, modify, distribute, and sell this software and its
552217195SDaniel Vetter  * documentation for any purpose is hereby granted without fee, provided that
652217195SDaniel Vetter  * the above copyright notice appear in all copies and that both that copyright
752217195SDaniel Vetter  * notice and this permission notice appear in supporting documentation, and
852217195SDaniel Vetter  * that the name of the copyright holders not be used in advertising or
952217195SDaniel Vetter  * publicity pertaining to distribution of the software without specific,
1052217195SDaniel Vetter  * written prior permission.  The copyright holders make no representations
1152217195SDaniel Vetter  * about the suitability of this software for any purpose.  It is provided "as
1252217195SDaniel Vetter  * is" without express or implied warranty.
1352217195SDaniel Vetter  *
1452217195SDaniel Vetter  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1552217195SDaniel Vetter  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1652217195SDaniel Vetter  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1752217195SDaniel Vetter  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1852217195SDaniel Vetter  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1952217195SDaniel Vetter  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2052217195SDaniel Vetter  * OF THIS SOFTWARE.
2152217195SDaniel Vetter  */
2252217195SDaniel Vetter 
2352217195SDaniel Vetter #include <drm/drm_connector.h>
2452217195SDaniel Vetter #include <drm/drm_edid.h>
259338203cSLaurent Pinchart #include <drm/drm_encoder.h>
268d70f395SHans de Goede #include <drm/drm_utils.h>
2799f45e32SDaniel Vetter #include <drm/drm_print.h>
2899f45e32SDaniel Vetter #include <drm/drm_drv.h>
2999f45e32SDaniel Vetter #include <drm/drm_file.h>
3099f45e32SDaniel Vetter 
3199f45e32SDaniel Vetter #include <linux/uaccess.h>
3252217195SDaniel Vetter 
3352217195SDaniel Vetter #include "drm_crtc_internal.h"
3452217195SDaniel Vetter #include "drm_internal.h"
3552217195SDaniel Vetter 
36ae2a6da8SDaniel Vetter /**
37ae2a6da8SDaniel Vetter  * DOC: overview
38ae2a6da8SDaniel Vetter  *
39ae2a6da8SDaniel Vetter  * In DRM connectors are the general abstraction for display sinks, and include
40ae2a6da8SDaniel Vetter  * als fixed panels or anything else that can display pixels in some form. As
41ae2a6da8SDaniel Vetter  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
42ae2a6da8SDaniel Vetter  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
43ad093607SThierry Reding  * Hence they are reference-counted using drm_connector_get() and
44ad093607SThierry Reding  * drm_connector_put().
45ae2a6da8SDaniel Vetter  *
46d574528aSDaniel Vetter  * KMS driver must create, initialize, register and attach at a &struct
47d574528aSDaniel Vetter  * drm_connector for each such sink. The instance is created as other KMS
48aec97460SDaniel Vetter  * objects and initialized by setting the following fields. The connector is
49aec97460SDaniel Vetter  * initialized with a call to drm_connector_init() with a pointer to the
50aec97460SDaniel Vetter  * &struct drm_connector_funcs and a connector type, and then exposed to
51aec97460SDaniel Vetter  * userspace with a call to drm_connector_register().
52ae2a6da8SDaniel Vetter  *
53ae2a6da8SDaniel Vetter  * Connectors must be attached to an encoder to be used. For devices that map
54ae2a6da8SDaniel Vetter  * connectors to encoders 1:1, the connector should be attached at
55cde4c44dSDaniel Vetter  * initialization time with a call to drm_connector_attach_encoder(). The
56d574528aSDaniel Vetter  * driver must also set the &drm_connector.encoder field to point to the
57ae2a6da8SDaniel Vetter  * attached encoder.
58ae2a6da8SDaniel Vetter  *
59ae2a6da8SDaniel Vetter  * For connectors which are not fixed (like built-in panels) the driver needs to
60ae2a6da8SDaniel Vetter  * support hotplug notifications. The simplest way to do that is by using the
61ae2a6da8SDaniel Vetter  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
62ae2a6da8SDaniel Vetter  * hardware support for hotplug interrupts. Connectors with hardware hotplug
63ae2a6da8SDaniel Vetter  * support can instead use e.g. drm_helper_hpd_irq_event().
64ae2a6da8SDaniel Vetter  */
65ae2a6da8SDaniel Vetter 
6652217195SDaniel Vetter struct drm_conn_prop_enum_list {
6752217195SDaniel Vetter 	int type;
6852217195SDaniel Vetter 	const char *name;
6952217195SDaniel Vetter 	struct ida ida;
7052217195SDaniel Vetter };
7152217195SDaniel Vetter 
7252217195SDaniel Vetter /*
7352217195SDaniel Vetter  * Connector and encoder types.
7452217195SDaniel Vetter  */
7552217195SDaniel Vetter static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
7652217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
7752217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
7852217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
7952217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
8052217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
8152217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
8252217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
8352217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
8452217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_Component, "Component" },
8552217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
8652217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
8752217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
8852217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
8952217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_TV, "TV" },
9052217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
9152217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
9252217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
9352217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
94935774cdSBrian Starkey 	{ DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
9552217195SDaniel Vetter };
9652217195SDaniel Vetter 
9752217195SDaniel Vetter void drm_connector_ida_init(void)
9852217195SDaniel Vetter {
9952217195SDaniel Vetter 	int i;
10052217195SDaniel Vetter 
10152217195SDaniel Vetter 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
10252217195SDaniel Vetter 		ida_init(&drm_connector_enum_list[i].ida);
10352217195SDaniel Vetter }
10452217195SDaniel Vetter 
10552217195SDaniel Vetter void drm_connector_ida_destroy(void)
10652217195SDaniel Vetter {
10752217195SDaniel Vetter 	int i;
10852217195SDaniel Vetter 
10952217195SDaniel Vetter 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
11052217195SDaniel Vetter 		ida_destroy(&drm_connector_enum_list[i].ida);
11152217195SDaniel Vetter }
11252217195SDaniel Vetter 
11352217195SDaniel Vetter /**
11452217195SDaniel Vetter  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
11552217195SDaniel Vetter  * @connector: connector to quwery
11652217195SDaniel Vetter  *
117ae2a6da8SDaniel Vetter  * The kernel supports per-connector configuration of its consoles through
11852217195SDaniel Vetter  * use of the video= parameter. This function parses that option and
11952217195SDaniel Vetter  * extracts the user's specified mode (or enable/disable status) for a
12052217195SDaniel Vetter  * particular connector. This is typically only used during the early fbdev
12152217195SDaniel Vetter  * setup.
12252217195SDaniel Vetter  */
12352217195SDaniel Vetter static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
12452217195SDaniel Vetter {
12552217195SDaniel Vetter 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
12652217195SDaniel Vetter 	char *option = NULL;
12752217195SDaniel Vetter 
12852217195SDaniel Vetter 	if (fb_get_options(connector->name, &option))
12952217195SDaniel Vetter 		return;
13052217195SDaniel Vetter 
13152217195SDaniel Vetter 	if (!drm_mode_parse_command_line_for_connector(option,
13252217195SDaniel Vetter 						       connector,
13352217195SDaniel Vetter 						       mode))
13452217195SDaniel Vetter 		return;
13552217195SDaniel Vetter 
13652217195SDaniel Vetter 	if (mode->force) {
1376140cf20SJani Nikula 		DRM_INFO("forcing %s connector %s\n", connector->name,
1386140cf20SJani Nikula 			 drm_get_connector_force_name(mode->force));
13952217195SDaniel Vetter 		connector->force = mode->force;
14052217195SDaniel Vetter 	}
14152217195SDaniel Vetter 
14252217195SDaniel Vetter 	DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
14352217195SDaniel Vetter 		      connector->name,
14452217195SDaniel Vetter 		      mode->xres, mode->yres,
14552217195SDaniel Vetter 		      mode->refresh_specified ? mode->refresh : 60,
14652217195SDaniel Vetter 		      mode->rb ? " reduced blanking" : "",
14752217195SDaniel Vetter 		      mode->margins ? " with margins" : "",
14852217195SDaniel Vetter 		      mode->interlace ?  " interlaced" : "");
14952217195SDaniel Vetter }
15052217195SDaniel Vetter 
15152217195SDaniel Vetter static void drm_connector_free(struct kref *kref)
15252217195SDaniel Vetter {
15352217195SDaniel Vetter 	struct drm_connector *connector =
15452217195SDaniel Vetter 		container_of(kref, struct drm_connector, base.refcount);
15552217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
15652217195SDaniel Vetter 
15752217195SDaniel Vetter 	drm_mode_object_unregister(dev, &connector->base);
15852217195SDaniel Vetter 	connector->funcs->destroy(connector);
15952217195SDaniel Vetter }
16052217195SDaniel Vetter 
161ea497bb9SDaniel Vetter void drm_connector_free_work_fn(struct work_struct *work)
162a703c550SDaniel Vetter {
163ea497bb9SDaniel Vetter 	struct drm_connector *connector, *n;
164ea497bb9SDaniel Vetter 	struct drm_device *dev =
165ea497bb9SDaniel Vetter 		container_of(work, struct drm_device, mode_config.connector_free_work);
166ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &dev->mode_config;
167ea497bb9SDaniel Vetter 	unsigned long flags;
168ea497bb9SDaniel Vetter 	struct llist_node *freed;
169a703c550SDaniel Vetter 
170ea497bb9SDaniel Vetter 	spin_lock_irqsave(&config->connector_list_lock, flags);
171ea497bb9SDaniel Vetter 	freed = llist_del_all(&config->connector_free_list);
172ea497bb9SDaniel Vetter 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
173ea497bb9SDaniel Vetter 
174ea497bb9SDaniel Vetter 	llist_for_each_entry_safe(connector, n, freed, free_node) {
175a703c550SDaniel Vetter 		drm_mode_object_unregister(dev, &connector->base);
176a703c550SDaniel Vetter 		connector->funcs->destroy(connector);
177a703c550SDaniel Vetter 	}
178ea497bb9SDaniel Vetter }
179a703c550SDaniel Vetter 
18052217195SDaniel Vetter /**
18152217195SDaniel Vetter  * drm_connector_init - Init a preallocated connector
18252217195SDaniel Vetter  * @dev: DRM device
18352217195SDaniel Vetter  * @connector: the connector to init
18452217195SDaniel Vetter  * @funcs: callbacks for this connector
18552217195SDaniel Vetter  * @connector_type: user visible type of the connector
18652217195SDaniel Vetter  *
18752217195SDaniel Vetter  * Initialises a preallocated connector. Connectors should be
18852217195SDaniel Vetter  * subclassed as part of driver connector objects.
18952217195SDaniel Vetter  *
19052217195SDaniel Vetter  * Returns:
19152217195SDaniel Vetter  * Zero on success, error code on failure.
19252217195SDaniel Vetter  */
19352217195SDaniel Vetter int drm_connector_init(struct drm_device *dev,
19452217195SDaniel Vetter 		       struct drm_connector *connector,
19552217195SDaniel Vetter 		       const struct drm_connector_funcs *funcs,
19652217195SDaniel Vetter 		       int connector_type)
19752217195SDaniel Vetter {
19852217195SDaniel Vetter 	struct drm_mode_config *config = &dev->mode_config;
19952217195SDaniel Vetter 	int ret;
20052217195SDaniel Vetter 	struct ida *connector_ida =
20152217195SDaniel Vetter 		&drm_connector_enum_list[connector_type].ida;
20252217195SDaniel Vetter 
203ba1f665fSHaneen Mohammed 	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
204ba1f665fSHaneen Mohammed 		(!funcs->atomic_destroy_state ||
205ba1f665fSHaneen Mohammed 		 !funcs->atomic_duplicate_state));
206ba1f665fSHaneen Mohammed 
2072135ea7aSThierry Reding 	ret = __drm_mode_object_add(dev, &connector->base,
20852217195SDaniel Vetter 				    DRM_MODE_OBJECT_CONNECTOR,
20952217195SDaniel Vetter 				    false, drm_connector_free);
21052217195SDaniel Vetter 	if (ret)
211613051daSDaniel Vetter 		return ret;
21252217195SDaniel Vetter 
21352217195SDaniel Vetter 	connector->base.properties = &connector->properties;
21452217195SDaniel Vetter 	connector->dev = dev;
21552217195SDaniel Vetter 	connector->funcs = funcs;
21652217195SDaniel Vetter 
2172a8d3eacSVille Syrjälä 	/* connector index is used with 32bit bitmasks */
2182a8d3eacSVille Syrjälä 	ret = ida_simple_get(&config->connector_ida, 0, 32, GFP_KERNEL);
2192a8d3eacSVille Syrjälä 	if (ret < 0) {
2202a8d3eacSVille Syrjälä 		DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
2212a8d3eacSVille Syrjälä 			      drm_connector_enum_list[connector_type].name,
2222a8d3eacSVille Syrjälä 			      ret);
22352217195SDaniel Vetter 		goto out_put;
2242a8d3eacSVille Syrjälä 	}
22552217195SDaniel Vetter 	connector->index = ret;
22652217195SDaniel Vetter 	ret = 0;
22752217195SDaniel Vetter 
22852217195SDaniel Vetter 	connector->connector_type = connector_type;
22952217195SDaniel Vetter 	connector->connector_type_id =
23052217195SDaniel Vetter 		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
23152217195SDaniel Vetter 	if (connector->connector_type_id < 0) {
23252217195SDaniel Vetter 		ret = connector->connector_type_id;
23352217195SDaniel Vetter 		goto out_put_id;
23452217195SDaniel Vetter 	}
23552217195SDaniel Vetter 	connector->name =
23652217195SDaniel Vetter 		kasprintf(GFP_KERNEL, "%s-%d",
23752217195SDaniel Vetter 			  drm_connector_enum_list[connector_type].name,
23852217195SDaniel Vetter 			  connector->connector_type_id);
23952217195SDaniel Vetter 	if (!connector->name) {
24052217195SDaniel Vetter 		ret = -ENOMEM;
24152217195SDaniel Vetter 		goto out_put_type_id;
24252217195SDaniel Vetter 	}
24352217195SDaniel Vetter 
24452217195SDaniel Vetter 	INIT_LIST_HEAD(&connector->probed_modes);
24552217195SDaniel Vetter 	INIT_LIST_HEAD(&connector->modes);
246e73ab00eSDaniel Vetter 	mutex_init(&connector->mutex);
24752217195SDaniel Vetter 	connector->edid_blob_ptr = NULL;
2482de3a078SManasi Navare 	connector->tile_blob_ptr = NULL;
24952217195SDaniel Vetter 	connector->status = connector_status_unknown;
2508d70f395SHans de Goede 	connector->display_info.panel_orientation =
2518d70f395SHans de Goede 		DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
25252217195SDaniel Vetter 
25352217195SDaniel Vetter 	drm_connector_get_cmdline_mode(connector);
25452217195SDaniel Vetter 
25552217195SDaniel Vetter 	/* We should add connectors at the end to avoid upsetting the connector
25652217195SDaniel Vetter 	 * index too much. */
257613051daSDaniel Vetter 	spin_lock_irq(&config->connector_list_lock);
25852217195SDaniel Vetter 	list_add_tail(&connector->head, &config->connector_list);
25952217195SDaniel Vetter 	config->num_connector++;
260613051daSDaniel Vetter 	spin_unlock_irq(&config->connector_list_lock);
26152217195SDaniel Vetter 
262935774cdSBrian Starkey 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
263935774cdSBrian Starkey 	    connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
2646b7e2d5cSGerd Hoffmann 		drm_connector_attach_edid_property(connector);
26552217195SDaniel Vetter 
26652217195SDaniel Vetter 	drm_object_attach_property(&connector->base,
26752217195SDaniel Vetter 				      config->dpms_property, 0);
26852217195SDaniel Vetter 
26940ee6fbeSManasi Navare 	drm_object_attach_property(&connector->base,
27040ee6fbeSManasi Navare 				   config->link_status_property,
27140ee6fbeSManasi Navare 				   0);
27240ee6fbeSManasi Navare 
27366660d4cSDave Airlie 	drm_object_attach_property(&connector->base,
27466660d4cSDave Airlie 				   config->non_desktop_property,
27566660d4cSDave Airlie 				   0);
2762de3a078SManasi Navare 	drm_object_attach_property(&connector->base,
2772de3a078SManasi Navare 				   config->tile_property,
2782de3a078SManasi Navare 				   0);
27966660d4cSDave Airlie 
28052217195SDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
28152217195SDaniel Vetter 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
28252217195SDaniel Vetter 	}
28352217195SDaniel Vetter 
28452217195SDaniel Vetter 	connector->debugfs_entry = NULL;
28552217195SDaniel Vetter out_put_type_id:
28652217195SDaniel Vetter 	if (ret)
287587680c1SChristophe JAILLET 		ida_simple_remove(connector_ida, connector->connector_type_id);
28852217195SDaniel Vetter out_put_id:
28952217195SDaniel Vetter 	if (ret)
290587680c1SChristophe JAILLET 		ida_simple_remove(&config->connector_ida, connector->index);
29152217195SDaniel Vetter out_put:
29252217195SDaniel Vetter 	if (ret)
29352217195SDaniel Vetter 		drm_mode_object_unregister(dev, &connector->base);
29452217195SDaniel Vetter 
29552217195SDaniel Vetter 	return ret;
29652217195SDaniel Vetter }
29752217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_init);
29852217195SDaniel Vetter 
29952217195SDaniel Vetter /**
3006b7e2d5cSGerd Hoffmann  * drm_connector_attach_edid_property - attach edid property.
3016b7e2d5cSGerd Hoffmann  * @connector: the connector
3026b7e2d5cSGerd Hoffmann  *
3036b7e2d5cSGerd Hoffmann  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
3046b7e2d5cSGerd Hoffmann  * edid property attached by default.  This function can be used to
3056b7e2d5cSGerd Hoffmann  * explicitly enable the edid property in these cases.
3066b7e2d5cSGerd Hoffmann  */
3076b7e2d5cSGerd Hoffmann void drm_connector_attach_edid_property(struct drm_connector *connector)
3086b7e2d5cSGerd Hoffmann {
3096b7e2d5cSGerd Hoffmann 	struct drm_mode_config *config = &connector->dev->mode_config;
3106b7e2d5cSGerd Hoffmann 
3116b7e2d5cSGerd Hoffmann 	drm_object_attach_property(&connector->base,
3126b7e2d5cSGerd Hoffmann 				   config->edid_property,
3136b7e2d5cSGerd Hoffmann 				   0);
3146b7e2d5cSGerd Hoffmann }
3156b7e2d5cSGerd Hoffmann EXPORT_SYMBOL(drm_connector_attach_edid_property);
3166b7e2d5cSGerd Hoffmann 
3176b7e2d5cSGerd Hoffmann /**
318cde4c44dSDaniel Vetter  * drm_connector_attach_encoder - attach a connector to an encoder
31952217195SDaniel Vetter  * @connector: connector to attach
32052217195SDaniel Vetter  * @encoder: encoder to attach @connector to
32152217195SDaniel Vetter  *
32252217195SDaniel Vetter  * This function links up a connector to an encoder. Note that the routing
32352217195SDaniel Vetter  * restrictions between encoders and crtcs are exposed to userspace through the
32452217195SDaniel Vetter  * possible_clones and possible_crtcs bitmasks.
32552217195SDaniel Vetter  *
32652217195SDaniel Vetter  * Returns:
32752217195SDaniel Vetter  * Zero on success, negative errno on failure.
32852217195SDaniel Vetter  */
329cde4c44dSDaniel Vetter int drm_connector_attach_encoder(struct drm_connector *connector,
33052217195SDaniel Vetter 				 struct drm_encoder *encoder)
33152217195SDaniel Vetter {
33252217195SDaniel Vetter 	int i;
33352217195SDaniel Vetter 
33452217195SDaniel Vetter 	/*
33552217195SDaniel Vetter 	 * In the past, drivers have attempted to model the static association
33652217195SDaniel Vetter 	 * of connector to encoder in simple connector/encoder devices using a
33752217195SDaniel Vetter 	 * direct assignment of connector->encoder = encoder. This connection
33852217195SDaniel Vetter 	 * is a logical one and the responsibility of the core, so drivers are
33952217195SDaniel Vetter 	 * expected not to mess with this.
34052217195SDaniel Vetter 	 *
34152217195SDaniel Vetter 	 * Note that the error return should've been enough here, but a large
34252217195SDaniel Vetter 	 * majority of drivers ignores the return value, so add in a big WARN
34352217195SDaniel Vetter 	 * to get people's attention.
34452217195SDaniel Vetter 	 */
34552217195SDaniel Vetter 	if (WARN_ON(connector->encoder))
34652217195SDaniel Vetter 		return -EINVAL;
34752217195SDaniel Vetter 
34883aefbb8SVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(connector->encoder_ids); i++) {
34952217195SDaniel Vetter 		if (connector->encoder_ids[i] == 0) {
35052217195SDaniel Vetter 			connector->encoder_ids[i] = encoder->base.id;
35152217195SDaniel Vetter 			return 0;
35252217195SDaniel Vetter 		}
35352217195SDaniel Vetter 	}
35452217195SDaniel Vetter 	return -ENOMEM;
35552217195SDaniel Vetter }
356cde4c44dSDaniel Vetter EXPORT_SYMBOL(drm_connector_attach_encoder);
35752217195SDaniel Vetter 
35838cb8d96SVille Syrjälä /**
35938cb8d96SVille Syrjälä  * drm_connector_has_possible_encoder - check if the connector and encoder are assosicated with each other
36038cb8d96SVille Syrjälä  * @connector: the connector
36138cb8d96SVille Syrjälä  * @encoder: the encoder
36238cb8d96SVille Syrjälä  *
36338cb8d96SVille Syrjälä  * Returns:
36438cb8d96SVille Syrjälä  * True if @encoder is one of the possible encoders for @connector.
36538cb8d96SVille Syrjälä  */
36638cb8d96SVille Syrjälä bool drm_connector_has_possible_encoder(struct drm_connector *connector,
36738cb8d96SVille Syrjälä 					struct drm_encoder *encoder)
36838cb8d96SVille Syrjälä {
36938cb8d96SVille Syrjälä 	struct drm_encoder *enc;
37038cb8d96SVille Syrjälä 	int i;
37138cb8d96SVille Syrjälä 
37238cb8d96SVille Syrjälä 	drm_connector_for_each_possible_encoder(connector, enc, i) {
37338cb8d96SVille Syrjälä 		if (enc == encoder)
37438cb8d96SVille Syrjälä 			return true;
37538cb8d96SVille Syrjälä 	}
37638cb8d96SVille Syrjälä 
37738cb8d96SVille Syrjälä 	return false;
37838cb8d96SVille Syrjälä }
37938cb8d96SVille Syrjälä EXPORT_SYMBOL(drm_connector_has_possible_encoder);
38038cb8d96SVille Syrjälä 
38152217195SDaniel Vetter static void drm_mode_remove(struct drm_connector *connector,
38252217195SDaniel Vetter 			    struct drm_display_mode *mode)
38352217195SDaniel Vetter {
38452217195SDaniel Vetter 	list_del(&mode->head);
38552217195SDaniel Vetter 	drm_mode_destroy(connector->dev, mode);
38652217195SDaniel Vetter }
38752217195SDaniel Vetter 
38852217195SDaniel Vetter /**
38952217195SDaniel Vetter  * drm_connector_cleanup - cleans up an initialised connector
39052217195SDaniel Vetter  * @connector: connector to cleanup
39152217195SDaniel Vetter  *
39252217195SDaniel Vetter  * Cleans up the connector but doesn't free the object.
39352217195SDaniel Vetter  */
39452217195SDaniel Vetter void drm_connector_cleanup(struct drm_connector *connector)
39552217195SDaniel Vetter {
39652217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
39752217195SDaniel Vetter 	struct drm_display_mode *mode, *t;
39852217195SDaniel Vetter 
39952217195SDaniel Vetter 	/* The connector should have been removed from userspace long before
40052217195SDaniel Vetter 	 * it is finally destroyed.
40152217195SDaniel Vetter 	 */
40239b50c60SLyude Paul 	if (WARN_ON(connector->registration_state ==
40339b50c60SLyude Paul 		    DRM_CONNECTOR_REGISTERED))
40452217195SDaniel Vetter 		drm_connector_unregister(connector);
40552217195SDaniel Vetter 
40652217195SDaniel Vetter 	if (connector->tile_group) {
40752217195SDaniel Vetter 		drm_mode_put_tile_group(dev, connector->tile_group);
40852217195SDaniel Vetter 		connector->tile_group = NULL;
40952217195SDaniel Vetter 	}
41052217195SDaniel Vetter 
41152217195SDaniel Vetter 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
41252217195SDaniel Vetter 		drm_mode_remove(connector, mode);
41352217195SDaniel Vetter 
41452217195SDaniel Vetter 	list_for_each_entry_safe(mode, t, &connector->modes, head)
41552217195SDaniel Vetter 		drm_mode_remove(connector, mode);
41652217195SDaniel Vetter 
4179a47dba1SChristophe JAILLET 	ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
41852217195SDaniel Vetter 			  connector->connector_type_id);
41952217195SDaniel Vetter 
4209a47dba1SChristophe JAILLET 	ida_simple_remove(&dev->mode_config.connector_ida,
42152217195SDaniel Vetter 			  connector->index);
42252217195SDaniel Vetter 
42352217195SDaniel Vetter 	kfree(connector->display_info.bus_formats);
42452217195SDaniel Vetter 	drm_mode_object_unregister(dev, &connector->base);
42552217195SDaniel Vetter 	kfree(connector->name);
42652217195SDaniel Vetter 	connector->name = NULL;
427613051daSDaniel Vetter 	spin_lock_irq(&dev->mode_config.connector_list_lock);
42852217195SDaniel Vetter 	list_del(&connector->head);
42952217195SDaniel Vetter 	dev->mode_config.num_connector--;
430613051daSDaniel Vetter 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
43152217195SDaniel Vetter 
43252217195SDaniel Vetter 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
43352217195SDaniel Vetter 	if (connector->state && connector->funcs->atomic_destroy_state)
43452217195SDaniel Vetter 		connector->funcs->atomic_destroy_state(connector,
43552217195SDaniel Vetter 						       connector->state);
43652217195SDaniel Vetter 
437e73ab00eSDaniel Vetter 	mutex_destroy(&connector->mutex);
438e73ab00eSDaniel Vetter 
43952217195SDaniel Vetter 	memset(connector, 0, sizeof(*connector));
44052217195SDaniel Vetter }
44152217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_cleanup);
44252217195SDaniel Vetter 
44352217195SDaniel Vetter /**
44452217195SDaniel Vetter  * drm_connector_register - register a connector
44552217195SDaniel Vetter  * @connector: the connector to register
44652217195SDaniel Vetter  *
44752217195SDaniel Vetter  * Register userspace interfaces for a connector
44852217195SDaniel Vetter  *
44952217195SDaniel Vetter  * Returns:
45052217195SDaniel Vetter  * Zero on success, error code on failure.
45152217195SDaniel Vetter  */
45252217195SDaniel Vetter int drm_connector_register(struct drm_connector *connector)
45352217195SDaniel Vetter {
454e73ab00eSDaniel Vetter 	int ret = 0;
45552217195SDaniel Vetter 
456e6e7b48bSDaniel Vetter 	if (!connector->dev->registered)
457e6e7b48bSDaniel Vetter 		return 0;
458e6e7b48bSDaniel Vetter 
459e73ab00eSDaniel Vetter 	mutex_lock(&connector->mutex);
46039b50c60SLyude Paul 	if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
461e73ab00eSDaniel Vetter 		goto unlock;
46252217195SDaniel Vetter 
46352217195SDaniel Vetter 	ret = drm_sysfs_connector_add(connector);
46452217195SDaniel Vetter 	if (ret)
465e73ab00eSDaniel Vetter 		goto unlock;
46652217195SDaniel Vetter 
467*b792e640SGreg Kroah-Hartman 	drm_debugfs_connector_add(connector);
46852217195SDaniel Vetter 
46952217195SDaniel Vetter 	if (connector->funcs->late_register) {
47052217195SDaniel Vetter 		ret = connector->funcs->late_register(connector);
47152217195SDaniel Vetter 		if (ret)
47252217195SDaniel Vetter 			goto err_debugfs;
47352217195SDaniel Vetter 	}
47452217195SDaniel Vetter 
47552217195SDaniel Vetter 	drm_mode_object_register(connector->dev, &connector->base);
47652217195SDaniel Vetter 
47739b50c60SLyude Paul 	connector->registration_state = DRM_CONNECTOR_REGISTERED;
478e73ab00eSDaniel Vetter 	goto unlock;
47952217195SDaniel Vetter 
48052217195SDaniel Vetter err_debugfs:
48152217195SDaniel Vetter 	drm_debugfs_connector_remove(connector);
48252217195SDaniel Vetter 	drm_sysfs_connector_remove(connector);
483e73ab00eSDaniel Vetter unlock:
484e73ab00eSDaniel Vetter 	mutex_unlock(&connector->mutex);
48552217195SDaniel Vetter 	return ret;
48652217195SDaniel Vetter }
48752217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_register);
48852217195SDaniel Vetter 
48952217195SDaniel Vetter /**
49052217195SDaniel Vetter  * drm_connector_unregister - unregister a connector
49152217195SDaniel Vetter  * @connector: the connector to unregister
49252217195SDaniel Vetter  *
49352217195SDaniel Vetter  * Unregister userspace interfaces for a connector
49452217195SDaniel Vetter  */
49552217195SDaniel Vetter void drm_connector_unregister(struct drm_connector *connector)
49652217195SDaniel Vetter {
497e73ab00eSDaniel Vetter 	mutex_lock(&connector->mutex);
49839b50c60SLyude Paul 	if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
499e73ab00eSDaniel Vetter 		mutex_unlock(&connector->mutex);
50052217195SDaniel Vetter 		return;
501e73ab00eSDaniel Vetter 	}
50252217195SDaniel Vetter 
50352217195SDaniel Vetter 	if (connector->funcs->early_unregister)
50452217195SDaniel Vetter 		connector->funcs->early_unregister(connector);
50552217195SDaniel Vetter 
50652217195SDaniel Vetter 	drm_sysfs_connector_remove(connector);
50752217195SDaniel Vetter 	drm_debugfs_connector_remove(connector);
50852217195SDaniel Vetter 
50939b50c60SLyude Paul 	connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
510e73ab00eSDaniel Vetter 	mutex_unlock(&connector->mutex);
51152217195SDaniel Vetter }
51252217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_unregister);
51352217195SDaniel Vetter 
51452217195SDaniel Vetter void drm_connector_unregister_all(struct drm_device *dev)
51552217195SDaniel Vetter {
51652217195SDaniel Vetter 	struct drm_connector *connector;
517613051daSDaniel Vetter 	struct drm_connector_list_iter conn_iter;
51852217195SDaniel Vetter 
519b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
520613051daSDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter)
52152217195SDaniel Vetter 		drm_connector_unregister(connector);
522b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
52352217195SDaniel Vetter }
52452217195SDaniel Vetter 
52552217195SDaniel Vetter int drm_connector_register_all(struct drm_device *dev)
52652217195SDaniel Vetter {
52752217195SDaniel Vetter 	struct drm_connector *connector;
528613051daSDaniel Vetter 	struct drm_connector_list_iter conn_iter;
529613051daSDaniel Vetter 	int ret = 0;
53052217195SDaniel Vetter 
531b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
532613051daSDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
53352217195SDaniel Vetter 		ret = drm_connector_register(connector);
53452217195SDaniel Vetter 		if (ret)
535613051daSDaniel Vetter 			break;
53652217195SDaniel Vetter 	}
537b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
53852217195SDaniel Vetter 
539613051daSDaniel Vetter 	if (ret)
54052217195SDaniel Vetter 		drm_connector_unregister_all(dev);
54152217195SDaniel Vetter 	return ret;
54252217195SDaniel Vetter }
54352217195SDaniel Vetter 
54452217195SDaniel Vetter /**
54552217195SDaniel Vetter  * drm_get_connector_status_name - return a string for connector status
54652217195SDaniel Vetter  * @status: connector status to compute name of
54752217195SDaniel Vetter  *
54852217195SDaniel Vetter  * In contrast to the other drm_get_*_name functions this one here returns a
54952217195SDaniel Vetter  * const pointer and hence is threadsafe.
55052217195SDaniel Vetter  */
55152217195SDaniel Vetter const char *drm_get_connector_status_name(enum drm_connector_status status)
55252217195SDaniel Vetter {
55352217195SDaniel Vetter 	if (status == connector_status_connected)
55452217195SDaniel Vetter 		return "connected";
55552217195SDaniel Vetter 	else if (status == connector_status_disconnected)
55652217195SDaniel Vetter 		return "disconnected";
55752217195SDaniel Vetter 	else
55852217195SDaniel Vetter 		return "unknown";
55952217195SDaniel Vetter }
56052217195SDaniel Vetter EXPORT_SYMBOL(drm_get_connector_status_name);
56152217195SDaniel Vetter 
5626140cf20SJani Nikula /**
5636140cf20SJani Nikula  * drm_get_connector_force_name - return a string for connector force
5646140cf20SJani Nikula  * @force: connector force to get name of
5656140cf20SJani Nikula  *
5666140cf20SJani Nikula  * Returns: const pointer to name.
5676140cf20SJani Nikula  */
5686140cf20SJani Nikula const char *drm_get_connector_force_name(enum drm_connector_force force)
5696140cf20SJani Nikula {
5706140cf20SJani Nikula 	switch (force) {
5716140cf20SJani Nikula 	case DRM_FORCE_UNSPECIFIED:
5726140cf20SJani Nikula 		return "unspecified";
5736140cf20SJani Nikula 	case DRM_FORCE_OFF:
5746140cf20SJani Nikula 		return "off";
5756140cf20SJani Nikula 	case DRM_FORCE_ON:
5766140cf20SJani Nikula 		return "on";
5776140cf20SJani Nikula 	case DRM_FORCE_ON_DIGITAL:
5786140cf20SJani Nikula 		return "digital";
5796140cf20SJani Nikula 	default:
5806140cf20SJani Nikula 		return "unknown";
5816140cf20SJani Nikula 	}
5826140cf20SJani Nikula }
5836140cf20SJani Nikula 
584613051daSDaniel Vetter #ifdef CONFIG_LOCKDEP
585613051daSDaniel Vetter static struct lockdep_map connector_list_iter_dep_map = {
586613051daSDaniel Vetter 	.name = "drm_connector_list_iter"
587613051daSDaniel Vetter };
588613051daSDaniel Vetter #endif
589613051daSDaniel Vetter 
590613051daSDaniel Vetter /**
591b982dab1SThierry Reding  * drm_connector_list_iter_begin - initialize a connector_list iterator
592613051daSDaniel Vetter  * @dev: DRM device
593613051daSDaniel Vetter  * @iter: connector_list iterator
594613051daSDaniel Vetter  *
595d574528aSDaniel Vetter  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
596b982dab1SThierry Reding  * must always be cleaned up again by calling drm_connector_list_iter_end().
597613051daSDaniel Vetter  * Iteration itself happens using drm_connector_list_iter_next() or
598613051daSDaniel Vetter  * drm_for_each_connector_iter().
599613051daSDaniel Vetter  */
600b982dab1SThierry Reding void drm_connector_list_iter_begin(struct drm_device *dev,
601613051daSDaniel Vetter 				   struct drm_connector_list_iter *iter)
602613051daSDaniel Vetter {
603613051daSDaniel Vetter 	iter->dev = dev;
604613051daSDaniel Vetter 	iter->conn = NULL;
605613051daSDaniel Vetter 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
606613051daSDaniel Vetter }
607b982dab1SThierry Reding EXPORT_SYMBOL(drm_connector_list_iter_begin);
608613051daSDaniel Vetter 
609a703c550SDaniel Vetter /*
610a703c550SDaniel Vetter  * Extra-safe connector put function that works in any context. Should only be
611a703c550SDaniel Vetter  * used from the connector_iter functions, where we never really expect to
612a703c550SDaniel Vetter  * actually release the connector when dropping our final reference.
613a703c550SDaniel Vetter  */
614a703c550SDaniel Vetter static void
615ea497bb9SDaniel Vetter __drm_connector_put_safe(struct drm_connector *conn)
616a703c550SDaniel Vetter {
617ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &conn->dev->mode_config;
618ea497bb9SDaniel Vetter 
619ea497bb9SDaniel Vetter 	lockdep_assert_held(&config->connector_list_lock);
620ea497bb9SDaniel Vetter 
621ea497bb9SDaniel Vetter 	if (!refcount_dec_and_test(&conn->base.refcount.refcount))
622ea497bb9SDaniel Vetter 		return;
623ea497bb9SDaniel Vetter 
624ea497bb9SDaniel Vetter 	llist_add(&conn->free_node, &config->connector_free_list);
625ea497bb9SDaniel Vetter 	schedule_work(&config->connector_free_work);
626a703c550SDaniel Vetter }
627a703c550SDaniel Vetter 
628613051daSDaniel Vetter /**
629613051daSDaniel Vetter  * drm_connector_list_iter_next - return next connector
6304f45c778SLyude Paul  * @iter: connector_list iterator
631613051daSDaniel Vetter  *
632613051daSDaniel Vetter  * Returns the next connector for @iter, or NULL when the list walk has
633613051daSDaniel Vetter  * completed.
634613051daSDaniel Vetter  */
635613051daSDaniel Vetter struct drm_connector *
636613051daSDaniel Vetter drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
637613051daSDaniel Vetter {
638613051daSDaniel Vetter 	struct drm_connector *old_conn = iter->conn;
639613051daSDaniel Vetter 	struct drm_mode_config *config = &iter->dev->mode_config;
640613051daSDaniel Vetter 	struct list_head *lhead;
641613051daSDaniel Vetter 	unsigned long flags;
642613051daSDaniel Vetter 
643613051daSDaniel Vetter 	spin_lock_irqsave(&config->connector_list_lock, flags);
644613051daSDaniel Vetter 	lhead = old_conn ? &old_conn->head : &config->connector_list;
645613051daSDaniel Vetter 
646613051daSDaniel Vetter 	do {
647613051daSDaniel Vetter 		if (lhead->next == &config->connector_list) {
648613051daSDaniel Vetter 			iter->conn = NULL;
649613051daSDaniel Vetter 			break;
650613051daSDaniel Vetter 		}
651613051daSDaniel Vetter 
652613051daSDaniel Vetter 		lhead = lhead->next;
653613051daSDaniel Vetter 		iter->conn = list_entry(lhead, struct drm_connector, head);
654613051daSDaniel Vetter 
655613051daSDaniel Vetter 		/* loop until it's not a zombie connector */
656613051daSDaniel Vetter 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
657613051daSDaniel Vetter 
658613051daSDaniel Vetter 	if (old_conn)
659ea497bb9SDaniel Vetter 		__drm_connector_put_safe(old_conn);
660ea497bb9SDaniel Vetter 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
661613051daSDaniel Vetter 
662613051daSDaniel Vetter 	return iter->conn;
663613051daSDaniel Vetter }
664613051daSDaniel Vetter EXPORT_SYMBOL(drm_connector_list_iter_next);
665613051daSDaniel Vetter 
666613051daSDaniel Vetter /**
667b982dab1SThierry Reding  * drm_connector_list_iter_end - tear down a connector_list iterator
668613051daSDaniel Vetter  * @iter: connector_list iterator
669613051daSDaniel Vetter  *
670613051daSDaniel Vetter  * Tears down @iter and releases any resources (like &drm_connector references)
671613051daSDaniel Vetter  * acquired while walking the list. This must always be called, both when the
672613051daSDaniel Vetter  * iteration completes fully or when it was aborted without walking the entire
673613051daSDaniel Vetter  * list.
674613051daSDaniel Vetter  */
675b982dab1SThierry Reding void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
676613051daSDaniel Vetter {
677ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &iter->dev->mode_config;
678ea497bb9SDaniel Vetter 	unsigned long flags;
679ea497bb9SDaniel Vetter 
680613051daSDaniel Vetter 	iter->dev = NULL;
681ea497bb9SDaniel Vetter 	if (iter->conn) {
682ea497bb9SDaniel Vetter 		spin_lock_irqsave(&config->connector_list_lock, flags);
683ea497bb9SDaniel Vetter 		__drm_connector_put_safe(iter->conn);
684ea497bb9SDaniel Vetter 		spin_unlock_irqrestore(&config->connector_list_lock, flags);
685ea497bb9SDaniel Vetter 	}
686613051daSDaniel Vetter 	lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
687613051daSDaniel Vetter }
688b982dab1SThierry Reding EXPORT_SYMBOL(drm_connector_list_iter_end);
689613051daSDaniel Vetter 
69052217195SDaniel Vetter static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
69152217195SDaniel Vetter 	{ SubPixelUnknown, "Unknown" },
69252217195SDaniel Vetter 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
69352217195SDaniel Vetter 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
69452217195SDaniel Vetter 	{ SubPixelVerticalRGB, "Vertical RGB" },
69552217195SDaniel Vetter 	{ SubPixelVerticalBGR, "Vertical BGR" },
69652217195SDaniel Vetter 	{ SubPixelNone, "None" },
69752217195SDaniel Vetter };
69852217195SDaniel Vetter 
69952217195SDaniel Vetter /**
70052217195SDaniel Vetter  * drm_get_subpixel_order_name - return a string for a given subpixel enum
70152217195SDaniel Vetter  * @order: enum of subpixel_order
70252217195SDaniel Vetter  *
70352217195SDaniel Vetter  * Note you could abuse this and return something out of bounds, but that
70452217195SDaniel Vetter  * would be a caller error.  No unscrubbed user data should make it here.
70552217195SDaniel Vetter  */
70652217195SDaniel Vetter const char *drm_get_subpixel_order_name(enum subpixel_order order)
70752217195SDaniel Vetter {
70852217195SDaniel Vetter 	return drm_subpixel_enum_list[order].name;
70952217195SDaniel Vetter }
71052217195SDaniel Vetter EXPORT_SYMBOL(drm_get_subpixel_order_name);
71152217195SDaniel Vetter 
71252217195SDaniel Vetter static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
71352217195SDaniel Vetter 	{ DRM_MODE_DPMS_ON, "On" },
71452217195SDaniel Vetter 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
71552217195SDaniel Vetter 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
71652217195SDaniel Vetter 	{ DRM_MODE_DPMS_OFF, "Off" }
71752217195SDaniel Vetter };
71852217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
71952217195SDaniel Vetter 
72040ee6fbeSManasi Navare static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
72140ee6fbeSManasi Navare 	{ DRM_MODE_LINK_STATUS_GOOD, "Good" },
72240ee6fbeSManasi Navare 	{ DRM_MODE_LINK_STATUS_BAD, "Bad" },
72340ee6fbeSManasi Navare };
72440ee6fbeSManasi Navare 
725b3c6c8bfSDaniel Vetter /**
726b3c6c8bfSDaniel Vetter  * drm_display_info_set_bus_formats - set the supported bus formats
727b3c6c8bfSDaniel Vetter  * @info: display info to store bus formats in
728b3c6c8bfSDaniel Vetter  * @formats: array containing the supported bus formats
729b3c6c8bfSDaniel Vetter  * @num_formats: the number of entries in the fmts array
730b3c6c8bfSDaniel Vetter  *
731b3c6c8bfSDaniel Vetter  * Store the supported bus formats in display info structure.
732b3c6c8bfSDaniel Vetter  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
733b3c6c8bfSDaniel Vetter  * a full list of available formats.
734b3c6c8bfSDaniel Vetter  */
735b3c6c8bfSDaniel Vetter int drm_display_info_set_bus_formats(struct drm_display_info *info,
736b3c6c8bfSDaniel Vetter 				     const u32 *formats,
737b3c6c8bfSDaniel Vetter 				     unsigned int num_formats)
738b3c6c8bfSDaniel Vetter {
739b3c6c8bfSDaniel Vetter 	u32 *fmts = NULL;
740b3c6c8bfSDaniel Vetter 
741b3c6c8bfSDaniel Vetter 	if (!formats && num_formats)
742b3c6c8bfSDaniel Vetter 		return -EINVAL;
743b3c6c8bfSDaniel Vetter 
744b3c6c8bfSDaniel Vetter 	if (formats && num_formats) {
745b3c6c8bfSDaniel Vetter 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
746b3c6c8bfSDaniel Vetter 			       GFP_KERNEL);
747b3c6c8bfSDaniel Vetter 		if (!fmts)
748b3c6c8bfSDaniel Vetter 			return -ENOMEM;
749b3c6c8bfSDaniel Vetter 	}
750b3c6c8bfSDaniel Vetter 
751b3c6c8bfSDaniel Vetter 	kfree(info->bus_formats);
752b3c6c8bfSDaniel Vetter 	info->bus_formats = fmts;
753b3c6c8bfSDaniel Vetter 	info->num_bus_formats = num_formats;
754b3c6c8bfSDaniel Vetter 
755b3c6c8bfSDaniel Vetter 	return 0;
756b3c6c8bfSDaniel Vetter }
757b3c6c8bfSDaniel Vetter EXPORT_SYMBOL(drm_display_info_set_bus_formats);
758b3c6c8bfSDaniel Vetter 
75952217195SDaniel Vetter /* Optional connector properties. */
76052217195SDaniel Vetter static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
76152217195SDaniel Vetter 	{ DRM_MODE_SCALE_NONE, "None" },
76252217195SDaniel Vetter 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
76352217195SDaniel Vetter 	{ DRM_MODE_SCALE_CENTER, "Center" },
76452217195SDaniel Vetter 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
76552217195SDaniel Vetter };
76652217195SDaniel Vetter 
76752217195SDaniel Vetter static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
76852217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
76952217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
77052217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
77152217195SDaniel Vetter };
77252217195SDaniel Vetter 
77350525c33SStanislav Lisovskiy static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
77450525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
77550525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
77650525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
77750525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
77850525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_GAME, "Game" },
77950525c33SStanislav Lisovskiy };
78050525c33SStanislav Lisovskiy 
7818d70f395SHans de Goede static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
7828d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_NORMAL,	"Normal"	},
7838d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP,	"Upside Down"	},
7848d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_LEFT_UP,	"Left Side Up"	},
7858d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,	"Right Side Up"	},
7868d70f395SHans de Goede };
7878d70f395SHans de Goede 
78852217195SDaniel Vetter static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
78952217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
79052217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
79152217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
79252217195SDaniel Vetter };
79352217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
79452217195SDaniel Vetter 
79552217195SDaniel Vetter static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
79652217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
79752217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
79852217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
79952217195SDaniel Vetter };
80052217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
80152217195SDaniel Vetter 		 drm_dvi_i_subconnector_enum_list)
80252217195SDaniel Vetter 
80352217195SDaniel Vetter static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
80452217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
80552217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
80652217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
80752217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
80852217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
80952217195SDaniel Vetter };
81052217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
81152217195SDaniel Vetter 
81252217195SDaniel Vetter static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
81352217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
81452217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
81552217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
81652217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
81752217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
81852217195SDaniel Vetter };
81952217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
82052217195SDaniel Vetter 		 drm_tv_subconnector_enum_list)
82152217195SDaniel Vetter 
822d2c6a405SUma Shankar static const struct drm_prop_enum_list hdmi_colorspaces[] = {
823d2c6a405SUma Shankar 	/* For Default case, driver will set the colorspace */
824d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
825d2c6a405SUma Shankar 	/* Standard Definition Colorimetry based on CEA 861 */
826d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_SMPTE_170M_YCC, "SMPTE_170M_YCC" },
827d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
828d2c6a405SUma Shankar 	/* Standard Definition Colorimetry based on IEC 61966-2-4 */
829d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
830d2c6a405SUma Shankar 	/* High Definition Colorimetry based on IEC 61966-2-4 */
831d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
832d2c6a405SUma Shankar 	/* Colorimetry based on IEC 61966-2-1/Amendment 1 */
833d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
834d2c6a405SUma Shankar 	/* Colorimetry based on IEC 61966-2-5 [33] */
835d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
836d2c6a405SUma Shankar 	/* Colorimetry based on IEC 61966-2-5 */
837d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
838d2c6a405SUma Shankar 	/* Colorimetry based on ITU-R BT.2020 */
839d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
840d2c6a405SUma Shankar 	/* Colorimetry based on ITU-R BT.2020 */
841d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
842d2c6a405SUma Shankar 	/* Colorimetry based on ITU-R BT.2020 */
843d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
844d2c6a405SUma Shankar 	/* Added as part of Additional Colorimetry Extension in 861.G */
845d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
846d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER, "DCI-P3_RGB_Theater" },
847d2c6a405SUma Shankar };
848d2c6a405SUma Shankar 
8494ada6f22SDaniel Vetter /**
8504ada6f22SDaniel Vetter  * DOC: standard connector properties
8514ada6f22SDaniel Vetter  *
8524ada6f22SDaniel Vetter  * DRM connectors have a few standardized properties:
8534ada6f22SDaniel Vetter  *
8544ada6f22SDaniel Vetter  * EDID:
8554ada6f22SDaniel Vetter  * 	Blob property which contains the current EDID read from the sink. This
8564ada6f22SDaniel Vetter  * 	is useful to parse sink identification information like vendor, model
8574ada6f22SDaniel Vetter  * 	and serial. Drivers should update this property by calling
858c555f023SDaniel Vetter  * 	drm_connector_update_edid_property(), usually after having parsed
8594ada6f22SDaniel Vetter  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
8604ada6f22SDaniel Vetter  * 	property.
8614ada6f22SDaniel Vetter  * DPMS:
8624ada6f22SDaniel Vetter  * 	Legacy property for setting the power state of the connector. For atomic
8634ada6f22SDaniel Vetter  * 	drivers this is only provided for backwards compatibility with existing
8644ada6f22SDaniel Vetter  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
8654ada6f22SDaniel Vetter  * 	connector is linked to. Drivers should never set this property directly,
866d574528aSDaniel Vetter  * 	it is handled by the DRM core by calling the &drm_connector_funcs.dpms
867144a7999SDaniel Vetter  * 	callback. For atomic drivers the remapping to the "ACTIVE" property is
868144a7999SDaniel Vetter  * 	implemented in the DRM core.  This is the only standard connector
869144a7999SDaniel Vetter  * 	property that userspace can change.
870d0d1aee5SDaniel Vetter  *
871d0d1aee5SDaniel Vetter  * 	Note that this property cannot be set through the MODE_ATOMIC ioctl,
872d0d1aee5SDaniel Vetter  * 	userspace must use "ACTIVE" on the CRTC instead.
873d0d1aee5SDaniel Vetter  *
874d0d1aee5SDaniel Vetter  * 	WARNING:
875d0d1aee5SDaniel Vetter  *
876d0d1aee5SDaniel Vetter  * 	For userspace also running on legacy drivers the "DPMS" semantics are a
877d0d1aee5SDaniel Vetter  * 	lot more complicated. First, userspace cannot rely on the "DPMS" value
878d0d1aee5SDaniel Vetter  * 	returned by the GETCONNECTOR actually reflecting reality, because many
879d0d1aee5SDaniel Vetter  * 	drivers fail to update it. For atomic drivers this is taken care of in
880d0d1aee5SDaniel Vetter  * 	drm_atomic_helper_update_legacy_modeset_state().
881d0d1aee5SDaniel Vetter  *
882d0d1aee5SDaniel Vetter  * 	The second issue is that the DPMS state is only well-defined when the
883d0d1aee5SDaniel Vetter  * 	connector is connected to a CRTC. In atomic the DRM core enforces that
884d0d1aee5SDaniel Vetter  * 	"ACTIVE" is off in such a case, no such checks exists for "DPMS".
885d0d1aee5SDaniel Vetter  *
886d0d1aee5SDaniel Vetter  * 	Finally, when enabling an output using the legacy SETCONFIG ioctl then
887d0d1aee5SDaniel Vetter  * 	"DPMS" is forced to ON. But see above, that might not be reflected in
888d0d1aee5SDaniel Vetter  * 	the software value on legacy drivers.
889d0d1aee5SDaniel Vetter  *
890d0d1aee5SDaniel Vetter  * 	Summarizing: Only set "DPMS" when the connector is known to be enabled,
891d0d1aee5SDaniel Vetter  * 	assume that a successful SETCONFIG call also sets "DPMS" to on, and
892d0d1aee5SDaniel Vetter  * 	never read back the value of "DPMS" because it can be incorrect.
8934ada6f22SDaniel Vetter  * PATH:
8944ada6f22SDaniel Vetter  * 	Connector path property to identify how this sink is physically
8954ada6f22SDaniel Vetter  * 	connected. Used by DP MST. This should be set by calling
89697e14fbeSDaniel Vetter  * 	drm_connector_set_path_property(), in the case of DP MST with the
8974ada6f22SDaniel Vetter  * 	path property the MST manager created. Userspace cannot change this
8984ada6f22SDaniel Vetter  * 	property.
8994ada6f22SDaniel Vetter  * TILE:
9004ada6f22SDaniel Vetter  * 	Connector tile group property to indicate how a set of DRM connector
9014ada6f22SDaniel Vetter  * 	compose together into one logical screen. This is used by both high-res
9024ada6f22SDaniel Vetter  * 	external screens (often only using a single cable, but exposing multiple
9034ada6f22SDaniel Vetter  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
9044ada6f22SDaniel Vetter  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
9054ada6f22SDaniel Vetter  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
9064ada6f22SDaniel Vetter  * 	tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
90797e14fbeSDaniel Vetter  * 	should update this value using drm_connector_set_tile_property().
9084ada6f22SDaniel Vetter  * 	Userspace cannot change this property.
90940ee6fbeSManasi Navare  * link-status:
910716719a3SSean Paul  *      Connector link-status property to indicate the status of link. The
911716719a3SSean Paul  *      default value of link-status is "GOOD". If something fails during or
912716719a3SSean Paul  *      after modeset, the kernel driver may set this to "BAD" and issue a
913716719a3SSean Paul  *      hotplug uevent. Drivers should update this value using
91497e14fbeSDaniel Vetter  *      drm_connector_set_link_status_property().
91566660d4cSDave Airlie  * non_desktop:
91666660d4cSDave Airlie  * 	Indicates the output should be ignored for purposes of displaying a
91766660d4cSDave Airlie  * 	standard desktop environment or console. This is most likely because
91866660d4cSDave Airlie  * 	the output device is not rectilinear.
91924557865SSean Paul  * Content Protection:
92024557865SSean Paul  *	This property is used by userspace to request the kernel protect future
92124557865SSean Paul  *	content communicated over the link. When requested, kernel will apply
92224557865SSean Paul  *	the appropriate means of protection (most often HDCP), and use the
92324557865SSean Paul  *	property to tell userspace the protection is active.
92424557865SSean Paul  *
92524557865SSean Paul  *	Drivers can set this up by calling
92624557865SSean Paul  *	drm_connector_attach_content_protection_property() on initialization.
92724557865SSean Paul  *
92824557865SSean Paul  *	The value of this property can be one of the following:
92924557865SSean Paul  *
930bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
93124557865SSean Paul  *		The link is not protected, content is transmitted in the clear.
932bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
93324557865SSean Paul  *		Userspace has requested content protection, but the link is not
93424557865SSean Paul  *		currently protected. When in this state, kernel should enable
93524557865SSean Paul  *		Content Protection as soon as possible.
936bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
93724557865SSean Paul  *		Userspace has requested content protection, and the link is
93824557865SSean Paul  *		protected. Only the driver can set the property to this value.
93924557865SSean Paul  *		If userspace attempts to set to ENABLED, kernel will return
94024557865SSean Paul  *		-EINVAL.
94124557865SSean Paul  *
94224557865SSean Paul  *	A few guidelines:
94324557865SSean Paul  *
94424557865SSean Paul  *	- DESIRED state should be preserved until userspace de-asserts it by
94524557865SSean Paul  *	  setting the property to UNDESIRED. This means ENABLED should only
94624557865SSean Paul  *	  transition to UNDESIRED when the user explicitly requests it.
94724557865SSean Paul  *	- If the state is DESIRED, kernel should attempt to re-authenticate the
94824557865SSean Paul  *	  link whenever possible. This includes across disable/enable, dpms,
94924557865SSean Paul  *	  hotplug, downstream device changes, link status failures, etc..
95024557865SSean Paul  *	- Userspace is responsible for polling the property to determine when
95124557865SSean Paul  *	  the value transitions from ENABLED to DESIRED. This signifies the link
95224557865SSean Paul  *	  is no longer protected and userspace should take appropriate action
95324557865SSean Paul  *	  (whatever that might be).
9544ada6f22SDaniel Vetter  *
955a09db883SUma Shankar  * HDR_OUTPUT_METADATA:
956a09db883SUma Shankar  *	Connector property to enable userspace to send HDR Metadata to
957a09db883SUma Shankar  *	driver. This metadata is based on the composition and blending
958a09db883SUma Shankar  *	policies decided by user, taking into account the hardware and
959a09db883SUma Shankar  *	sink capabilities. The driver gets this metadata and creates a
960a09db883SUma Shankar  *	Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
961a09db883SUma Shankar  *	SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
962a09db883SUma Shankar  *	sent to sink. This notifies the sink of the upcoming frame's Color
963a09db883SUma Shankar  *	Encoding and Luminance parameters.
964a09db883SUma Shankar  *
965a09db883SUma Shankar  *	Userspace first need to detect the HDR capabilities of sink by
966a09db883SUma Shankar  *	reading and parsing the EDID. Details of HDR metadata for HDMI
967a09db883SUma Shankar  *	are added in CTA 861.G spec. For DP , its defined in VESA DP
968a09db883SUma Shankar  *	Standard v1.4. It needs to then get the metadata information
969a09db883SUma Shankar  *	of the video/game/app content which are encoded in HDR (basically
970a09db883SUma Shankar  *	using HDR transfer functions). With this information it needs to
971a09db883SUma Shankar  *	decide on a blending policy and compose the relevant
972a09db883SUma Shankar  *	layers/overlays into a common format. Once this blending is done,
973a09db883SUma Shankar  *	userspace will be aware of the metadata of the composed frame to
974a09db883SUma Shankar  *	be send to sink. It then uses this property to communicate this
975a09db883SUma Shankar  *	metadata to driver which then make a Infoframe packet and sends
976a09db883SUma Shankar  *	to sink based on the type of encoder connected.
977a09db883SUma Shankar  *
978a09db883SUma Shankar  *	Userspace will be responsible to do Tone mapping operation in case:
979a09db883SUma Shankar  *		- Some layers are HDR and others are SDR
980a09db883SUma Shankar  *		- HDR layers luminance is not same as sink
981a09db883SUma Shankar  *	It will even need to do colorspace conversion and get all layers
982a09db883SUma Shankar  *	to one common colorspace for blending. It can use either GL, Media
983a09db883SUma Shankar  *	or display engine to get this done based on the capabilties of the
984a09db883SUma Shankar  *	associated hardware.
985a09db883SUma Shankar  *
986a09db883SUma Shankar  *	Driver expects metadata to be put in &struct hdr_output_metadata
987a09db883SUma Shankar  *	structure from userspace. This is received as blob and stored in
988a09db883SUma Shankar  *	&drm_connector_state.hdr_output_metadata. It parses EDID and saves the
989a09db883SUma Shankar  *	sink metadata in &struct hdr_sink_metadata, as
990a09db883SUma Shankar  *	&drm_connector.hdr_sink_metadata.  Driver uses
991a09db883SUma Shankar  *	drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
992a09db883SUma Shankar  *	hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
993a09db883SUma Shankar  *	HDMI encoder.
994a09db883SUma Shankar  *
99547e22ff1SRadhakrishna Sripada  * max bpc:
99647e22ff1SRadhakrishna Sripada  *	This range property is used by userspace to limit the bit depth. When
99747e22ff1SRadhakrishna Sripada  *	used the driver would limit the bpc in accordance with the valid range
99847e22ff1SRadhakrishna Sripada  *	supported by the hardware and sink. Drivers to use the function
99947e22ff1SRadhakrishna Sripada  *	drm_connector_attach_max_bpc_property() to create and attach the
100047e22ff1SRadhakrishna Sripada  *	property to the connector during initialization.
100147e22ff1SRadhakrishna Sripada  *
10024ada6f22SDaniel Vetter  * Connectors also have one standardized atomic property:
10034ada6f22SDaniel Vetter  *
10044ada6f22SDaniel Vetter  * CRTC_ID:
10054ada6f22SDaniel Vetter  * 	Mode object ID of the &drm_crtc this connector should be connected to.
10068d70f395SHans de Goede  *
10078d70f395SHans de Goede  * Connectors for LCD panels may also have one standardized property:
10088d70f395SHans de Goede  *
10098d70f395SHans de Goede  * panel orientation:
10108d70f395SHans de Goede  *	On some devices the LCD panel is mounted in the casing in such a way
10118d70f395SHans de Goede  *	that the up/top side of the panel does not match with the top side of
10128d70f395SHans de Goede  *	the device. Userspace can use this property to check for this.
10138d70f395SHans de Goede  *	Note that input coordinates from touchscreens (input devices with
10148d70f395SHans de Goede  *	INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
10158d70f395SHans de Goede  *	coordinates, so if userspace rotates the picture to adjust for
10168d70f395SHans de Goede  *	the orientation it must also apply the same transformation to the
1017bbeba09fSDaniel Vetter  *	touchscreen input coordinates. This property is initialized by calling
1018bbeba09fSDaniel Vetter  *	drm_connector_init_panel_orientation_property().
1019bbeba09fSDaniel Vetter  *
1020bbeba09fSDaniel Vetter  * scaling mode:
1021bbeba09fSDaniel Vetter  *	This property defines how a non-native mode is upscaled to the native
1022bbeba09fSDaniel Vetter  *	mode of an LCD panel:
1023bbeba09fSDaniel Vetter  *
1024bbeba09fSDaniel Vetter  *	None:
1025bbeba09fSDaniel Vetter  *		No upscaling happens, scaling is left to the panel. Not all
1026bbeba09fSDaniel Vetter  *		drivers expose this mode.
1027bbeba09fSDaniel Vetter  *	Full:
1028bbeba09fSDaniel Vetter  *		The output is upscaled to the full resolution of the panel,
1029bbeba09fSDaniel Vetter  *		ignoring the aspect ratio.
1030bbeba09fSDaniel Vetter  *	Center:
1031bbeba09fSDaniel Vetter  *		No upscaling happens, the output is centered within the native
1032bbeba09fSDaniel Vetter  *		resolution the panel.
1033bbeba09fSDaniel Vetter  *	Full aspect:
1034bbeba09fSDaniel Vetter  *		The output is upscaled to maximize either the width or height
1035bbeba09fSDaniel Vetter  *		while retaining the aspect ratio.
1036bbeba09fSDaniel Vetter  *
1037bbeba09fSDaniel Vetter  *	This property should be set up by calling
1038bbeba09fSDaniel Vetter  *	drm_connector_attach_scaling_mode_property(). Note that drivers
1039bbeba09fSDaniel Vetter  *	can also expose this property to external outputs, in which case they
1040bbeba09fSDaniel Vetter  *	must support "None", which should be the default (since external screens
1041bbeba09fSDaniel Vetter  *	have a built-in scaler).
10424ada6f22SDaniel Vetter  */
10434ada6f22SDaniel Vetter 
104452217195SDaniel Vetter int drm_connector_create_standard_properties(struct drm_device *dev)
104552217195SDaniel Vetter {
104652217195SDaniel Vetter 	struct drm_property *prop;
104752217195SDaniel Vetter 
104852217195SDaniel Vetter 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
104952217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
105052217195SDaniel Vetter 				   "EDID", 0);
105152217195SDaniel Vetter 	if (!prop)
105252217195SDaniel Vetter 		return -ENOMEM;
105352217195SDaniel Vetter 	dev->mode_config.edid_property = prop;
105452217195SDaniel Vetter 
105552217195SDaniel Vetter 	prop = drm_property_create_enum(dev, 0,
105652217195SDaniel Vetter 				   "DPMS", drm_dpms_enum_list,
105752217195SDaniel Vetter 				   ARRAY_SIZE(drm_dpms_enum_list));
105852217195SDaniel Vetter 	if (!prop)
105952217195SDaniel Vetter 		return -ENOMEM;
106052217195SDaniel Vetter 	dev->mode_config.dpms_property = prop;
106152217195SDaniel Vetter 
106252217195SDaniel Vetter 	prop = drm_property_create(dev,
106352217195SDaniel Vetter 				   DRM_MODE_PROP_BLOB |
106452217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
106552217195SDaniel Vetter 				   "PATH", 0);
106652217195SDaniel Vetter 	if (!prop)
106752217195SDaniel Vetter 		return -ENOMEM;
106852217195SDaniel Vetter 	dev->mode_config.path_property = prop;
106952217195SDaniel Vetter 
107052217195SDaniel Vetter 	prop = drm_property_create(dev,
107152217195SDaniel Vetter 				   DRM_MODE_PROP_BLOB |
107252217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
107352217195SDaniel Vetter 				   "TILE", 0);
107452217195SDaniel Vetter 	if (!prop)
107552217195SDaniel Vetter 		return -ENOMEM;
107652217195SDaniel Vetter 	dev->mode_config.tile_property = prop;
107752217195SDaniel Vetter 
107840ee6fbeSManasi Navare 	prop = drm_property_create_enum(dev, 0, "link-status",
107940ee6fbeSManasi Navare 					drm_link_status_enum_list,
108040ee6fbeSManasi Navare 					ARRAY_SIZE(drm_link_status_enum_list));
108140ee6fbeSManasi Navare 	if (!prop)
108240ee6fbeSManasi Navare 		return -ENOMEM;
108340ee6fbeSManasi Navare 	dev->mode_config.link_status_property = prop;
108440ee6fbeSManasi Navare 
108566660d4cSDave Airlie 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
108666660d4cSDave Airlie 	if (!prop)
108766660d4cSDave Airlie 		return -ENOMEM;
108866660d4cSDave Airlie 	dev->mode_config.non_desktop_property = prop;
108966660d4cSDave Airlie 
1090fbb5d035SUma Shankar 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1091fbb5d035SUma Shankar 				   "HDR_OUTPUT_METADATA", 0);
1092fbb5d035SUma Shankar 	if (!prop)
1093fbb5d035SUma Shankar 		return -ENOMEM;
1094fbb5d035SUma Shankar 	dev->mode_config.hdr_output_metadata_property = prop;
1095fbb5d035SUma Shankar 
109652217195SDaniel Vetter 	return 0;
109752217195SDaniel Vetter }
109852217195SDaniel Vetter 
109952217195SDaniel Vetter /**
110052217195SDaniel Vetter  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
110152217195SDaniel Vetter  * @dev: DRM device
110252217195SDaniel Vetter  *
110352217195SDaniel Vetter  * Called by a driver the first time a DVI-I connector is made.
110452217195SDaniel Vetter  */
110552217195SDaniel Vetter int drm_mode_create_dvi_i_properties(struct drm_device *dev)
110652217195SDaniel Vetter {
110752217195SDaniel Vetter 	struct drm_property *dvi_i_selector;
110852217195SDaniel Vetter 	struct drm_property *dvi_i_subconnector;
110952217195SDaniel Vetter 
111052217195SDaniel Vetter 	if (dev->mode_config.dvi_i_select_subconnector_property)
111152217195SDaniel Vetter 		return 0;
111252217195SDaniel Vetter 
111352217195SDaniel Vetter 	dvi_i_selector =
111452217195SDaniel Vetter 		drm_property_create_enum(dev, 0,
111552217195SDaniel Vetter 				    "select subconnector",
111652217195SDaniel Vetter 				    drm_dvi_i_select_enum_list,
111752217195SDaniel Vetter 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
111852217195SDaniel Vetter 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
111952217195SDaniel Vetter 
112052217195SDaniel Vetter 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
112152217195SDaniel Vetter 				    "subconnector",
112252217195SDaniel Vetter 				    drm_dvi_i_subconnector_enum_list,
112352217195SDaniel Vetter 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
112452217195SDaniel Vetter 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
112552217195SDaniel Vetter 
112652217195SDaniel Vetter 	return 0;
112752217195SDaniel Vetter }
112852217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
112952217195SDaniel Vetter 
113052217195SDaniel Vetter /**
113150525c33SStanislav Lisovskiy  * DOC: HDMI connector properties
113250525c33SStanislav Lisovskiy  *
113350525c33SStanislav Lisovskiy  * content type (HDMI specific):
113450525c33SStanislav Lisovskiy  *	Indicates content type setting to be used in HDMI infoframes to indicate
11351e55a53aSMatt Roper  *	content type for the external device, so that it adjusts its display
113650525c33SStanislav Lisovskiy  *	settings accordingly.
113750525c33SStanislav Lisovskiy  *
113850525c33SStanislav Lisovskiy  *	The value of this property can be one of the following:
113950525c33SStanislav Lisovskiy  *
114050525c33SStanislav Lisovskiy  *	No Data:
114150525c33SStanislav Lisovskiy  *		Content type is unknown
114250525c33SStanislav Lisovskiy  *	Graphics:
114350525c33SStanislav Lisovskiy  *		Content type is graphics
114450525c33SStanislav Lisovskiy  *	Photo:
114550525c33SStanislav Lisovskiy  *		Content type is photo
114650525c33SStanislav Lisovskiy  *	Cinema:
114750525c33SStanislav Lisovskiy  *		Content type is cinema
114850525c33SStanislav Lisovskiy  *	Game:
114950525c33SStanislav Lisovskiy  *		Content type is game
115050525c33SStanislav Lisovskiy  *
115150525c33SStanislav Lisovskiy  *	Drivers can set up this property by calling
115250525c33SStanislav Lisovskiy  *	drm_connector_attach_content_type_property(). Decoding to
1153ba609631SDaniel Vetter  *	infoframe values is done through drm_hdmi_avi_infoframe_content_type().
115450525c33SStanislav Lisovskiy  */
115550525c33SStanislav Lisovskiy 
115650525c33SStanislav Lisovskiy /**
115750525c33SStanislav Lisovskiy  * drm_connector_attach_content_type_property - attach content-type property
115850525c33SStanislav Lisovskiy  * @connector: connector to attach content type property on.
115950525c33SStanislav Lisovskiy  *
116050525c33SStanislav Lisovskiy  * Called by a driver the first time a HDMI connector is made.
116150525c33SStanislav Lisovskiy  */
116250525c33SStanislav Lisovskiy int drm_connector_attach_content_type_property(struct drm_connector *connector)
116350525c33SStanislav Lisovskiy {
116450525c33SStanislav Lisovskiy 	if (!drm_mode_create_content_type_property(connector->dev))
116550525c33SStanislav Lisovskiy 		drm_object_attach_property(&connector->base,
116650525c33SStanislav Lisovskiy 					   connector->dev->mode_config.content_type_property,
116750525c33SStanislav Lisovskiy 					   DRM_MODE_CONTENT_TYPE_NO_DATA);
116850525c33SStanislav Lisovskiy 	return 0;
116950525c33SStanislav Lisovskiy }
117050525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_connector_attach_content_type_property);
117150525c33SStanislav Lisovskiy 
117250525c33SStanislav Lisovskiy 
117350525c33SStanislav Lisovskiy /**
117450525c33SStanislav Lisovskiy  * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
117550525c33SStanislav Lisovskiy  *                                         content type information, based
117650525c33SStanislav Lisovskiy  *                                         on correspondent DRM property.
117750525c33SStanislav Lisovskiy  * @frame: HDMI AVI infoframe
117850525c33SStanislav Lisovskiy  * @conn_state: DRM display connector state
117950525c33SStanislav Lisovskiy  *
118050525c33SStanislav Lisovskiy  */
118150525c33SStanislav Lisovskiy void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
118250525c33SStanislav Lisovskiy 					 const struct drm_connector_state *conn_state)
118350525c33SStanislav Lisovskiy {
118450525c33SStanislav Lisovskiy 	switch (conn_state->content_type) {
118550525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_GRAPHICS:
118650525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
118750525c33SStanislav Lisovskiy 		break;
118850525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_CINEMA:
118950525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
119050525c33SStanislav Lisovskiy 		break;
119150525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_GAME:
119250525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GAME;
119350525c33SStanislav Lisovskiy 		break;
119450525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_PHOTO:
119550525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
119650525c33SStanislav Lisovskiy 		break;
119750525c33SStanislav Lisovskiy 	default:
119850525c33SStanislav Lisovskiy 		/* Graphics is the default(0) */
119950525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
120050525c33SStanislav Lisovskiy 	}
120150525c33SStanislav Lisovskiy 
120250525c33SStanislav Lisovskiy 	frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
120350525c33SStanislav Lisovskiy }
120450525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
120550525c33SStanislav Lisovskiy 
120650525c33SStanislav Lisovskiy /**
12076c4f52dcSBoris Brezillon  * drm_mode_attach_tv_margin_properties - attach TV connector margin properties
12086c4f52dcSBoris Brezillon  * @connector: DRM connector
12096c4f52dcSBoris Brezillon  *
12106c4f52dcSBoris Brezillon  * Called by a driver when it needs to attach TV margin props to a connector.
12116c4f52dcSBoris Brezillon  * Typically used on SDTV and HDMI connectors.
12126c4f52dcSBoris Brezillon  */
12136c4f52dcSBoris Brezillon void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
12146c4f52dcSBoris Brezillon {
12156c4f52dcSBoris Brezillon 	struct drm_device *dev = connector->dev;
12166c4f52dcSBoris Brezillon 
12176c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
12186c4f52dcSBoris Brezillon 				   dev->mode_config.tv_left_margin_property,
12196c4f52dcSBoris Brezillon 				   0);
12206c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
12216c4f52dcSBoris Brezillon 				   dev->mode_config.tv_right_margin_property,
12226c4f52dcSBoris Brezillon 				   0);
12236c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
12246c4f52dcSBoris Brezillon 				   dev->mode_config.tv_top_margin_property,
12256c4f52dcSBoris Brezillon 				   0);
12266c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
12276c4f52dcSBoris Brezillon 				   dev->mode_config.tv_bottom_margin_property,
12286c4f52dcSBoris Brezillon 				   0);
12296c4f52dcSBoris Brezillon }
12306c4f52dcSBoris Brezillon EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
12316c4f52dcSBoris Brezillon 
12326c4f52dcSBoris Brezillon /**
12336c4f52dcSBoris Brezillon  * drm_mode_create_tv_margin_properties - create TV connector margin properties
12346c4f52dcSBoris Brezillon  * @dev: DRM device
12356c4f52dcSBoris Brezillon  *
12366c4f52dcSBoris Brezillon  * Called by a driver's HDMI connector initialization routine, this function
12376c4f52dcSBoris Brezillon  * creates the TV margin properties for a given device. No need to call this
12386c4f52dcSBoris Brezillon  * function for an SDTV connector, it's already called from
12396c4f52dcSBoris Brezillon  * drm_mode_create_tv_properties().
12406c4f52dcSBoris Brezillon  */
12416c4f52dcSBoris Brezillon int drm_mode_create_tv_margin_properties(struct drm_device *dev)
12426c4f52dcSBoris Brezillon {
12436c4f52dcSBoris Brezillon 	if (dev->mode_config.tv_left_margin_property)
12446c4f52dcSBoris Brezillon 		return 0;
12456c4f52dcSBoris Brezillon 
12466c4f52dcSBoris Brezillon 	dev->mode_config.tv_left_margin_property =
12476c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "left margin", 0, 100);
12486c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_left_margin_property)
12496c4f52dcSBoris Brezillon 		return -ENOMEM;
12506c4f52dcSBoris Brezillon 
12516c4f52dcSBoris Brezillon 	dev->mode_config.tv_right_margin_property =
12526c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "right margin", 0, 100);
12536c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_right_margin_property)
12546c4f52dcSBoris Brezillon 		return -ENOMEM;
12556c4f52dcSBoris Brezillon 
12566c4f52dcSBoris Brezillon 	dev->mode_config.tv_top_margin_property =
12576c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "top margin", 0, 100);
12586c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_top_margin_property)
12596c4f52dcSBoris Brezillon 		return -ENOMEM;
12606c4f52dcSBoris Brezillon 
12616c4f52dcSBoris Brezillon 	dev->mode_config.tv_bottom_margin_property =
12626c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
12636c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_bottom_margin_property)
12646c4f52dcSBoris Brezillon 		return -ENOMEM;
12656c4f52dcSBoris Brezillon 
12666c4f52dcSBoris Brezillon 	return 0;
12676c4f52dcSBoris Brezillon }
12686c4f52dcSBoris Brezillon EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
12696c4f52dcSBoris Brezillon 
12706c4f52dcSBoris Brezillon /**
1271eda6887fSBoris Brezillon  * drm_mode_create_tv_properties - create TV specific connector properties
127252217195SDaniel Vetter  * @dev: DRM device
127352217195SDaniel Vetter  * @num_modes: number of different TV formats (modes) supported
127452217195SDaniel Vetter  * @modes: array of pointers to strings containing name of each format
127552217195SDaniel Vetter  *
127652217195SDaniel Vetter  * Called by a driver's TV initialization routine, this function creates
127752217195SDaniel Vetter  * the TV specific connector properties for a given device.  Caller is
127852217195SDaniel Vetter  * responsible for allocating a list of format names and passing them to
127952217195SDaniel Vetter  * this routine.
128052217195SDaniel Vetter  */
128152217195SDaniel Vetter int drm_mode_create_tv_properties(struct drm_device *dev,
128252217195SDaniel Vetter 				  unsigned int num_modes,
128352217195SDaniel Vetter 				  const char * const modes[])
128452217195SDaniel Vetter {
128552217195SDaniel Vetter 	struct drm_property *tv_selector;
128652217195SDaniel Vetter 	struct drm_property *tv_subconnector;
128752217195SDaniel Vetter 	unsigned int i;
128852217195SDaniel Vetter 
128952217195SDaniel Vetter 	if (dev->mode_config.tv_select_subconnector_property)
129052217195SDaniel Vetter 		return 0;
129152217195SDaniel Vetter 
129252217195SDaniel Vetter 	/*
129352217195SDaniel Vetter 	 * Basic connector properties
129452217195SDaniel Vetter 	 */
129552217195SDaniel Vetter 	tv_selector = drm_property_create_enum(dev, 0,
129652217195SDaniel Vetter 					  "select subconnector",
129752217195SDaniel Vetter 					  drm_tv_select_enum_list,
129852217195SDaniel Vetter 					  ARRAY_SIZE(drm_tv_select_enum_list));
129952217195SDaniel Vetter 	if (!tv_selector)
130052217195SDaniel Vetter 		goto nomem;
130152217195SDaniel Vetter 
130252217195SDaniel Vetter 	dev->mode_config.tv_select_subconnector_property = tv_selector;
130352217195SDaniel Vetter 
130452217195SDaniel Vetter 	tv_subconnector =
130552217195SDaniel Vetter 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
130652217195SDaniel Vetter 				    "subconnector",
130752217195SDaniel Vetter 				    drm_tv_subconnector_enum_list,
130852217195SDaniel Vetter 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
130952217195SDaniel Vetter 	if (!tv_subconnector)
131052217195SDaniel Vetter 		goto nomem;
131152217195SDaniel Vetter 	dev->mode_config.tv_subconnector_property = tv_subconnector;
131252217195SDaniel Vetter 
131352217195SDaniel Vetter 	/*
131452217195SDaniel Vetter 	 * Other, TV specific properties: margins & TV modes.
131552217195SDaniel Vetter 	 */
13166c4f52dcSBoris Brezillon 	if (drm_mode_create_tv_margin_properties(dev))
131752217195SDaniel Vetter 		goto nomem;
131852217195SDaniel Vetter 
131952217195SDaniel Vetter 	dev->mode_config.tv_mode_property =
132052217195SDaniel Vetter 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
132152217195SDaniel Vetter 				    "mode", num_modes);
132252217195SDaniel Vetter 	if (!dev->mode_config.tv_mode_property)
132352217195SDaniel Vetter 		goto nomem;
132452217195SDaniel Vetter 
132552217195SDaniel Vetter 	for (i = 0; i < num_modes; i++)
132630e9db6dSVille Syrjälä 		drm_property_add_enum(dev->mode_config.tv_mode_property,
132752217195SDaniel Vetter 				      i, modes[i]);
132852217195SDaniel Vetter 
132952217195SDaniel Vetter 	dev->mode_config.tv_brightness_property =
133052217195SDaniel Vetter 		drm_property_create_range(dev, 0, "brightness", 0, 100);
133152217195SDaniel Vetter 	if (!dev->mode_config.tv_brightness_property)
133252217195SDaniel Vetter 		goto nomem;
133352217195SDaniel Vetter 
133452217195SDaniel Vetter 	dev->mode_config.tv_contrast_property =
133552217195SDaniel Vetter 		drm_property_create_range(dev, 0, "contrast", 0, 100);
133652217195SDaniel Vetter 	if (!dev->mode_config.tv_contrast_property)
133752217195SDaniel Vetter 		goto nomem;
133852217195SDaniel Vetter 
133952217195SDaniel Vetter 	dev->mode_config.tv_flicker_reduction_property =
134052217195SDaniel Vetter 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
134152217195SDaniel Vetter 	if (!dev->mode_config.tv_flicker_reduction_property)
134252217195SDaniel Vetter 		goto nomem;
134352217195SDaniel Vetter 
134452217195SDaniel Vetter 	dev->mode_config.tv_overscan_property =
134552217195SDaniel Vetter 		drm_property_create_range(dev, 0, "overscan", 0, 100);
134652217195SDaniel Vetter 	if (!dev->mode_config.tv_overscan_property)
134752217195SDaniel Vetter 		goto nomem;
134852217195SDaniel Vetter 
134952217195SDaniel Vetter 	dev->mode_config.tv_saturation_property =
135052217195SDaniel Vetter 		drm_property_create_range(dev, 0, "saturation", 0, 100);
135152217195SDaniel Vetter 	if (!dev->mode_config.tv_saturation_property)
135252217195SDaniel Vetter 		goto nomem;
135352217195SDaniel Vetter 
135452217195SDaniel Vetter 	dev->mode_config.tv_hue_property =
135552217195SDaniel Vetter 		drm_property_create_range(dev, 0, "hue", 0, 100);
135652217195SDaniel Vetter 	if (!dev->mode_config.tv_hue_property)
135752217195SDaniel Vetter 		goto nomem;
135852217195SDaniel Vetter 
135952217195SDaniel Vetter 	return 0;
136052217195SDaniel Vetter nomem:
136152217195SDaniel Vetter 	return -ENOMEM;
136252217195SDaniel Vetter }
136352217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_tv_properties);
136452217195SDaniel Vetter 
136552217195SDaniel Vetter /**
136652217195SDaniel Vetter  * drm_mode_create_scaling_mode_property - create scaling mode property
136752217195SDaniel Vetter  * @dev: DRM device
136852217195SDaniel Vetter  *
136952217195SDaniel Vetter  * Called by a driver the first time it's needed, must be attached to desired
137052217195SDaniel Vetter  * connectors.
13718f6e1e22SMaarten Lankhorst  *
13728f6e1e22SMaarten Lankhorst  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
13738f6e1e22SMaarten Lankhorst  * instead to correctly assign &drm_connector_state.picture_aspect_ratio
13748f6e1e22SMaarten Lankhorst  * in the atomic state.
137552217195SDaniel Vetter  */
137652217195SDaniel Vetter int drm_mode_create_scaling_mode_property(struct drm_device *dev)
137752217195SDaniel Vetter {
137852217195SDaniel Vetter 	struct drm_property *scaling_mode;
137952217195SDaniel Vetter 
138052217195SDaniel Vetter 	if (dev->mode_config.scaling_mode_property)
138152217195SDaniel Vetter 		return 0;
138252217195SDaniel Vetter 
138352217195SDaniel Vetter 	scaling_mode =
138452217195SDaniel Vetter 		drm_property_create_enum(dev, 0, "scaling mode",
138552217195SDaniel Vetter 				drm_scaling_mode_enum_list,
138652217195SDaniel Vetter 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
138752217195SDaniel Vetter 
138852217195SDaniel Vetter 	dev->mode_config.scaling_mode_property = scaling_mode;
138952217195SDaniel Vetter 
139052217195SDaniel Vetter 	return 0;
139152217195SDaniel Vetter }
139252217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
139352217195SDaniel Vetter 
139452217195SDaniel Vetter /**
1395ab7a664fSNicholas Kazlauskas  * DOC: Variable refresh properties
1396ab7a664fSNicholas Kazlauskas  *
1397ab7a664fSNicholas Kazlauskas  * Variable refresh rate capable displays can dynamically adjust their
1398ab7a664fSNicholas Kazlauskas  * refresh rate by extending the duration of their vertical front porch
1399ab7a664fSNicholas Kazlauskas  * until page flip or timeout occurs. This can reduce or remove stuttering
1400ab7a664fSNicholas Kazlauskas  * and latency in scenarios where the page flip does not align with the
1401ab7a664fSNicholas Kazlauskas  * vblank interval.
1402ab7a664fSNicholas Kazlauskas  *
1403ab7a664fSNicholas Kazlauskas  * An example scenario would be an application flipping at a constant rate
1404ab7a664fSNicholas Kazlauskas  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
1405ab7a664fSNicholas Kazlauskas  * interval and the same contents will be displayed twice. This can be
1406ab7a664fSNicholas Kazlauskas  * observed as stuttering for content with motion.
1407ab7a664fSNicholas Kazlauskas  *
1408ab7a664fSNicholas Kazlauskas  * If variable refresh rate was active on a display that supported a
1409ab7a664fSNicholas Kazlauskas  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
1410ab7a664fSNicholas Kazlauskas  * for the example scenario. The minimum supported variable refresh rate of
1411ab7a664fSNicholas Kazlauskas  * 35Hz is below the page flip frequency and the vertical front porch can
1412ab7a664fSNicholas Kazlauskas  * be extended until the page flip occurs. The vblank interval will be
1413ab7a664fSNicholas Kazlauskas  * directly aligned to the page flip rate.
1414ab7a664fSNicholas Kazlauskas  *
1415ab7a664fSNicholas Kazlauskas  * Not all userspace content is suitable for use with variable refresh rate.
1416ab7a664fSNicholas Kazlauskas  * Large and frequent changes in vertical front porch duration may worsen
1417ab7a664fSNicholas Kazlauskas  * perceived stuttering for input sensitive applications.
1418ab7a664fSNicholas Kazlauskas  *
1419ab7a664fSNicholas Kazlauskas  * Panel brightness will also vary with vertical front porch duration. Some
1420ab7a664fSNicholas Kazlauskas  * panels may have noticeable differences in brightness between the minimum
1421ab7a664fSNicholas Kazlauskas  * vertical front porch duration and the maximum vertical front porch duration.
1422ab7a664fSNicholas Kazlauskas  * Large and frequent changes in vertical front porch duration may produce
1423ab7a664fSNicholas Kazlauskas  * observable flickering for such panels.
1424ab7a664fSNicholas Kazlauskas  *
1425ab7a664fSNicholas Kazlauskas  * Userspace control for variable refresh rate is supported via properties
1426ab7a664fSNicholas Kazlauskas  * on the &drm_connector and &drm_crtc objects.
1427ab7a664fSNicholas Kazlauskas  *
1428ab7a664fSNicholas Kazlauskas  * "vrr_capable":
1429ab7a664fSNicholas Kazlauskas  *	Optional &drm_connector boolean property that drivers should attach
1430ab7a664fSNicholas Kazlauskas  *	with drm_connector_attach_vrr_capable_property() on connectors that
1431ab7a664fSNicholas Kazlauskas  *	could support variable refresh rates. Drivers should update the
1432ab7a664fSNicholas Kazlauskas  *	property value by calling drm_connector_set_vrr_capable_property().
1433ab7a664fSNicholas Kazlauskas  *
1434ab7a664fSNicholas Kazlauskas  *	Absence of the property should indicate absence of support.
1435ab7a664fSNicholas Kazlauskas  *
143677086014SDaniel Vetter  * "VRR_ENABLED":
1437ab7a664fSNicholas Kazlauskas  *	Default &drm_crtc boolean property that notifies the driver that the
1438ab7a664fSNicholas Kazlauskas  *	content on the CRTC is suitable for variable refresh rate presentation.
1439ab7a664fSNicholas Kazlauskas  *	The driver will take this property as a hint to enable variable
1440ab7a664fSNicholas Kazlauskas  *	refresh rate support if the receiver supports it, ie. if the
1441ab7a664fSNicholas Kazlauskas  *	"vrr_capable" property is true on the &drm_connector object. The
1442ab7a664fSNicholas Kazlauskas  *	vertical front porch duration will be extended until page-flip or
1443ab7a664fSNicholas Kazlauskas  *	timeout when enabled.
1444ab7a664fSNicholas Kazlauskas  *
1445ab7a664fSNicholas Kazlauskas  *	The minimum vertical front porch duration is defined as the vertical
1446ab7a664fSNicholas Kazlauskas  *	front porch duration for the current mode.
1447ab7a664fSNicholas Kazlauskas  *
1448ab7a664fSNicholas Kazlauskas  *	The maximum vertical front porch duration is greater than or equal to
1449ab7a664fSNicholas Kazlauskas  *	the minimum vertical front porch duration. The duration is derived
1450ab7a664fSNicholas Kazlauskas  *	from the minimum supported variable refresh rate for the connector.
1451ab7a664fSNicholas Kazlauskas  *
1452ab7a664fSNicholas Kazlauskas  *	The driver may place further restrictions within these minimum
1453ab7a664fSNicholas Kazlauskas  *	and maximum bounds.
1454ab7a664fSNicholas Kazlauskas  */
1455ab7a664fSNicholas Kazlauskas 
1456ab7a664fSNicholas Kazlauskas /**
1457ba1b0f6cSNicholas Kazlauskas  * drm_connector_attach_vrr_capable_property - creates the
1458ba1b0f6cSNicholas Kazlauskas  * vrr_capable property
1459ba1b0f6cSNicholas Kazlauskas  * @connector: connector to create the vrr_capable property on.
1460ba1b0f6cSNicholas Kazlauskas  *
1461ba1b0f6cSNicholas Kazlauskas  * This is used by atomic drivers to add support for querying
1462ba1b0f6cSNicholas Kazlauskas  * variable refresh rate capability for a connector.
1463ba1b0f6cSNicholas Kazlauskas  *
1464ba1b0f6cSNicholas Kazlauskas  * Returns:
1465ba1b0f6cSNicholas Kazlauskas  * Zero on success, negative errono on failure.
1466ba1b0f6cSNicholas Kazlauskas  */
1467ba1b0f6cSNicholas Kazlauskas int drm_connector_attach_vrr_capable_property(
1468ba1b0f6cSNicholas Kazlauskas 	struct drm_connector *connector)
1469ba1b0f6cSNicholas Kazlauskas {
1470ba1b0f6cSNicholas Kazlauskas 	struct drm_device *dev = connector->dev;
1471ba1b0f6cSNicholas Kazlauskas 	struct drm_property *prop;
1472ba1b0f6cSNicholas Kazlauskas 
1473ba1b0f6cSNicholas Kazlauskas 	if (!connector->vrr_capable_property) {
1474ba1b0f6cSNicholas Kazlauskas 		prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
1475ba1b0f6cSNicholas Kazlauskas 			"vrr_capable");
1476ba1b0f6cSNicholas Kazlauskas 		if (!prop)
1477ba1b0f6cSNicholas Kazlauskas 			return -ENOMEM;
1478ba1b0f6cSNicholas Kazlauskas 
1479ba1b0f6cSNicholas Kazlauskas 		connector->vrr_capable_property = prop;
1480ba1b0f6cSNicholas Kazlauskas 		drm_object_attach_property(&connector->base, prop, 0);
1481ba1b0f6cSNicholas Kazlauskas 	}
1482ba1b0f6cSNicholas Kazlauskas 
1483ba1b0f6cSNicholas Kazlauskas 	return 0;
1484ba1b0f6cSNicholas Kazlauskas }
1485ba1b0f6cSNicholas Kazlauskas EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
1486ba1b0f6cSNicholas Kazlauskas 
1487ba1b0f6cSNicholas Kazlauskas /**
14888f6e1e22SMaarten Lankhorst  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
14898f6e1e22SMaarten Lankhorst  * @connector: connector to attach scaling mode property on.
14908f6e1e22SMaarten Lankhorst  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
14918f6e1e22SMaarten Lankhorst  *
14928f6e1e22SMaarten Lankhorst  * This is used to add support for scaling mode to atomic drivers.
14938f6e1e22SMaarten Lankhorst  * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
14948f6e1e22SMaarten Lankhorst  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
14958f6e1e22SMaarten Lankhorst  *
14968f6e1e22SMaarten Lankhorst  * This is the atomic version of drm_mode_create_scaling_mode_property().
14978f6e1e22SMaarten Lankhorst  *
14988f6e1e22SMaarten Lankhorst  * Returns:
14998f6e1e22SMaarten Lankhorst  * Zero on success, negative errno on failure.
15008f6e1e22SMaarten Lankhorst  */
15018f6e1e22SMaarten Lankhorst int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
15028f6e1e22SMaarten Lankhorst 					       u32 scaling_mode_mask)
15038f6e1e22SMaarten Lankhorst {
15048f6e1e22SMaarten Lankhorst 	struct drm_device *dev = connector->dev;
15058f6e1e22SMaarten Lankhorst 	struct drm_property *scaling_mode_property;
150630e9db6dSVille Syrjälä 	int i;
15078f6e1e22SMaarten Lankhorst 	const unsigned valid_scaling_mode_mask =
15088f6e1e22SMaarten Lankhorst 		(1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
15098f6e1e22SMaarten Lankhorst 
15108f6e1e22SMaarten Lankhorst 	if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
15118f6e1e22SMaarten Lankhorst 		    scaling_mode_mask & ~valid_scaling_mode_mask))
15128f6e1e22SMaarten Lankhorst 		return -EINVAL;
15138f6e1e22SMaarten Lankhorst 
15148f6e1e22SMaarten Lankhorst 	scaling_mode_property =
15158f6e1e22SMaarten Lankhorst 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
15168f6e1e22SMaarten Lankhorst 				    hweight32(scaling_mode_mask));
15178f6e1e22SMaarten Lankhorst 
15188f6e1e22SMaarten Lankhorst 	if (!scaling_mode_property)
15198f6e1e22SMaarten Lankhorst 		return -ENOMEM;
15208f6e1e22SMaarten Lankhorst 
15218f6e1e22SMaarten Lankhorst 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
15228f6e1e22SMaarten Lankhorst 		int ret;
15238f6e1e22SMaarten Lankhorst 
15248f6e1e22SMaarten Lankhorst 		if (!(BIT(i) & scaling_mode_mask))
15258f6e1e22SMaarten Lankhorst 			continue;
15268f6e1e22SMaarten Lankhorst 
152730e9db6dSVille Syrjälä 		ret = drm_property_add_enum(scaling_mode_property,
15288f6e1e22SMaarten Lankhorst 					    drm_scaling_mode_enum_list[i].type,
15298f6e1e22SMaarten Lankhorst 					    drm_scaling_mode_enum_list[i].name);
15308f6e1e22SMaarten Lankhorst 
15318f6e1e22SMaarten Lankhorst 		if (ret) {
15328f6e1e22SMaarten Lankhorst 			drm_property_destroy(dev, scaling_mode_property);
15338f6e1e22SMaarten Lankhorst 
15348f6e1e22SMaarten Lankhorst 			return ret;
15358f6e1e22SMaarten Lankhorst 		}
15368f6e1e22SMaarten Lankhorst 	}
15378f6e1e22SMaarten Lankhorst 
15388f6e1e22SMaarten Lankhorst 	drm_object_attach_property(&connector->base,
15398f6e1e22SMaarten Lankhorst 				   scaling_mode_property, 0);
15408f6e1e22SMaarten Lankhorst 
15418f6e1e22SMaarten Lankhorst 	connector->scaling_mode_property = scaling_mode_property;
15428f6e1e22SMaarten Lankhorst 
15438f6e1e22SMaarten Lankhorst 	return 0;
15448f6e1e22SMaarten Lankhorst }
15458f6e1e22SMaarten Lankhorst EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
15468f6e1e22SMaarten Lankhorst 
15478f6e1e22SMaarten Lankhorst /**
154852217195SDaniel Vetter  * drm_mode_create_aspect_ratio_property - create aspect ratio property
154952217195SDaniel Vetter  * @dev: DRM device
155052217195SDaniel Vetter  *
155152217195SDaniel Vetter  * Called by a driver the first time it's needed, must be attached to desired
155252217195SDaniel Vetter  * connectors.
155352217195SDaniel Vetter  *
155452217195SDaniel Vetter  * Returns:
155552217195SDaniel Vetter  * Zero on success, negative errno on failure.
155652217195SDaniel Vetter  */
155752217195SDaniel Vetter int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
155852217195SDaniel Vetter {
155952217195SDaniel Vetter 	if (dev->mode_config.aspect_ratio_property)
156052217195SDaniel Vetter 		return 0;
156152217195SDaniel Vetter 
156252217195SDaniel Vetter 	dev->mode_config.aspect_ratio_property =
156352217195SDaniel Vetter 		drm_property_create_enum(dev, 0, "aspect ratio",
156452217195SDaniel Vetter 				drm_aspect_ratio_enum_list,
156552217195SDaniel Vetter 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
156652217195SDaniel Vetter 
156752217195SDaniel Vetter 	if (dev->mode_config.aspect_ratio_property == NULL)
156852217195SDaniel Vetter 		return -ENOMEM;
156952217195SDaniel Vetter 
157052217195SDaniel Vetter 	return 0;
157152217195SDaniel Vetter }
157252217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
157352217195SDaniel Vetter 
157452217195SDaniel Vetter /**
1575d2c6a405SUma Shankar  * DOC: standard connector properties
1576d2c6a405SUma Shankar  *
1577d2c6a405SUma Shankar  * Colorspace:
1578d2c6a405SUma Shankar  *     drm_mode_create_colorspace_property - create colorspace property
1579d2c6a405SUma Shankar  *     This property helps select a suitable colorspace based on the sink
1580d2c6a405SUma Shankar  *     capability. Modern sink devices support wider gamut like BT2020.
1581d2c6a405SUma Shankar  *     This helps switch to BT2020 mode if the BT2020 encoded video stream
1582d2c6a405SUma Shankar  *     is being played by the user, same for any other colorspace. Thereby
1583d2c6a405SUma Shankar  *     giving a good visual experience to users.
1584d2c6a405SUma Shankar  *
1585d2c6a405SUma Shankar  *     The expectation from userspace is that it should parse the EDID
1586d2c6a405SUma Shankar  *     and get supported colorspaces. Use this property and switch to the
1587d2c6a405SUma Shankar  *     one supported. Sink supported colorspaces should be retrieved by
1588d2c6a405SUma Shankar  *     userspace from EDID and driver will not explicitly expose them.
1589d2c6a405SUma Shankar  *
1590d2c6a405SUma Shankar  *     Basically the expectation from userspace is:
1591d2c6a405SUma Shankar  *      - Set up CRTC DEGAMMA/CTM/GAMMA to convert to some sink
1592d2c6a405SUma Shankar  *        colorspace
1593d2c6a405SUma Shankar  *      - Set this new property to let the sink know what it
1594d2c6a405SUma Shankar  *        converted the CRTC output to.
1595d2c6a405SUma Shankar  *      - This property is just to inform sink what colorspace
1596d2c6a405SUma Shankar  *        source is trying to drive.
1597d2c6a405SUma Shankar  *
1598d2c6a405SUma Shankar  * Called by a driver the first time it's needed, must be attached to desired
1599d2c6a405SUma Shankar  * connectors.
1600d2c6a405SUma Shankar  */
1601d2c6a405SUma Shankar int drm_mode_create_colorspace_property(struct drm_connector *connector)
1602d2c6a405SUma Shankar {
1603d2c6a405SUma Shankar 	struct drm_device *dev = connector->dev;
1604d2c6a405SUma Shankar 	struct drm_property *prop;
1605d2c6a405SUma Shankar 
1606d2c6a405SUma Shankar 	if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
1607d2c6a405SUma Shankar 	    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
1608d2c6a405SUma Shankar 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
1609d2c6a405SUma Shankar 						"Colorspace",
1610d2c6a405SUma Shankar 						hdmi_colorspaces,
1611d2c6a405SUma Shankar 						ARRAY_SIZE(hdmi_colorspaces));
1612d2c6a405SUma Shankar 		if (!prop)
1613d2c6a405SUma Shankar 			return -ENOMEM;
1614d2c6a405SUma Shankar 	} else {
1615d2c6a405SUma Shankar 		DRM_DEBUG_KMS("Colorspace property not supported\n");
1616d2c6a405SUma Shankar 		return 0;
1617d2c6a405SUma Shankar 	}
1618d2c6a405SUma Shankar 
1619d2c6a405SUma Shankar 	connector->colorspace_property = prop;
1620d2c6a405SUma Shankar 
1621d2c6a405SUma Shankar 	return 0;
1622d2c6a405SUma Shankar }
1623d2c6a405SUma Shankar EXPORT_SYMBOL(drm_mode_create_colorspace_property);
1624d2c6a405SUma Shankar 
1625d2c6a405SUma Shankar /**
162650525c33SStanislav Lisovskiy  * drm_mode_create_content_type_property - create content type property
162750525c33SStanislav Lisovskiy  * @dev: DRM device
162850525c33SStanislav Lisovskiy  *
162950525c33SStanislav Lisovskiy  * Called by a driver the first time it's needed, must be attached to desired
163050525c33SStanislav Lisovskiy  * connectors.
163150525c33SStanislav Lisovskiy  *
163250525c33SStanislav Lisovskiy  * Returns:
163350525c33SStanislav Lisovskiy  * Zero on success, negative errno on failure.
163450525c33SStanislav Lisovskiy  */
163550525c33SStanislav Lisovskiy int drm_mode_create_content_type_property(struct drm_device *dev)
163650525c33SStanislav Lisovskiy {
163750525c33SStanislav Lisovskiy 	if (dev->mode_config.content_type_property)
163850525c33SStanislav Lisovskiy 		return 0;
163950525c33SStanislav Lisovskiy 
164050525c33SStanislav Lisovskiy 	dev->mode_config.content_type_property =
164150525c33SStanislav Lisovskiy 		drm_property_create_enum(dev, 0, "content type",
164250525c33SStanislav Lisovskiy 					 drm_content_type_enum_list,
164350525c33SStanislav Lisovskiy 					 ARRAY_SIZE(drm_content_type_enum_list));
164450525c33SStanislav Lisovskiy 
164550525c33SStanislav Lisovskiy 	if (dev->mode_config.content_type_property == NULL)
164650525c33SStanislav Lisovskiy 		return -ENOMEM;
164750525c33SStanislav Lisovskiy 
164850525c33SStanislav Lisovskiy 	return 0;
164950525c33SStanislav Lisovskiy }
165050525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_mode_create_content_type_property);
165150525c33SStanislav Lisovskiy 
165250525c33SStanislav Lisovskiy /**
165352217195SDaniel Vetter  * drm_mode_create_suggested_offset_properties - create suggests offset properties
165452217195SDaniel Vetter  * @dev: DRM device
165552217195SDaniel Vetter  *
165652217195SDaniel Vetter  * Create the the suggested x/y offset property for connectors.
165752217195SDaniel Vetter  */
165852217195SDaniel Vetter int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
165952217195SDaniel Vetter {
166052217195SDaniel Vetter 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
166152217195SDaniel Vetter 		return 0;
166252217195SDaniel Vetter 
166352217195SDaniel Vetter 	dev->mode_config.suggested_x_property =
166452217195SDaniel Vetter 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
166552217195SDaniel Vetter 
166652217195SDaniel Vetter 	dev->mode_config.suggested_y_property =
166752217195SDaniel Vetter 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
166852217195SDaniel Vetter 
166952217195SDaniel Vetter 	if (dev->mode_config.suggested_x_property == NULL ||
167052217195SDaniel Vetter 	    dev->mode_config.suggested_y_property == NULL)
167152217195SDaniel Vetter 		return -ENOMEM;
167252217195SDaniel Vetter 	return 0;
167352217195SDaniel Vetter }
167452217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
167552217195SDaniel Vetter 
167652217195SDaniel Vetter /**
167797e14fbeSDaniel Vetter  * drm_connector_set_path_property - set tile property on connector
167852217195SDaniel Vetter  * @connector: connector to set property on.
167952217195SDaniel Vetter  * @path: path to use for property; must not be NULL.
168052217195SDaniel Vetter  *
168152217195SDaniel Vetter  * This creates a property to expose to userspace to specify a
168252217195SDaniel Vetter  * connector path. This is mainly used for DisplayPort MST where
168352217195SDaniel Vetter  * connectors have a topology and we want to allow userspace to give
168452217195SDaniel Vetter  * them more meaningful names.
168552217195SDaniel Vetter  *
168652217195SDaniel Vetter  * Returns:
168752217195SDaniel Vetter  * Zero on success, negative errno on failure.
168852217195SDaniel Vetter  */
168997e14fbeSDaniel Vetter int drm_connector_set_path_property(struct drm_connector *connector,
169052217195SDaniel Vetter 				    const char *path)
169152217195SDaniel Vetter {
169252217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
169352217195SDaniel Vetter 	int ret;
169452217195SDaniel Vetter 
169552217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
169652217195SDaniel Vetter 	                                       &connector->path_blob_ptr,
169752217195SDaniel Vetter 	                                       strlen(path) + 1,
169852217195SDaniel Vetter 	                                       path,
169952217195SDaniel Vetter 	                                       &connector->base,
170052217195SDaniel Vetter 	                                       dev->mode_config.path_property);
170152217195SDaniel Vetter 	return ret;
170252217195SDaniel Vetter }
170397e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_path_property);
170452217195SDaniel Vetter 
170552217195SDaniel Vetter /**
170697e14fbeSDaniel Vetter  * drm_connector_set_tile_property - set tile property on connector
170752217195SDaniel Vetter  * @connector: connector to set property on.
170852217195SDaniel Vetter  *
170952217195SDaniel Vetter  * This looks up the tile information for a connector, and creates a
171052217195SDaniel Vetter  * property for userspace to parse if it exists. The property is of
171152217195SDaniel Vetter  * the form of 8 integers using ':' as a separator.
17122de3a078SManasi Navare  * This is used for dual port tiled displays with DisplayPort SST
17132de3a078SManasi Navare  * or DisplayPort MST connectors.
171452217195SDaniel Vetter  *
171552217195SDaniel Vetter  * Returns:
171652217195SDaniel Vetter  * Zero on success, errno on failure.
171752217195SDaniel Vetter  */
171897e14fbeSDaniel Vetter int drm_connector_set_tile_property(struct drm_connector *connector)
171952217195SDaniel Vetter {
172052217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
172152217195SDaniel Vetter 	char tile[256];
172252217195SDaniel Vetter 	int ret;
172352217195SDaniel Vetter 
172452217195SDaniel Vetter 	if (!connector->has_tile) {
172552217195SDaniel Vetter 		ret  = drm_property_replace_global_blob(dev,
172652217195SDaniel Vetter 		                                        &connector->tile_blob_ptr,
172752217195SDaniel Vetter 		                                        0,
172852217195SDaniel Vetter 		                                        NULL,
172952217195SDaniel Vetter 		                                        &connector->base,
173052217195SDaniel Vetter 		                                        dev->mode_config.tile_property);
173152217195SDaniel Vetter 		return ret;
173252217195SDaniel Vetter 	}
173352217195SDaniel Vetter 
173452217195SDaniel Vetter 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
173552217195SDaniel Vetter 		 connector->tile_group->id, connector->tile_is_single_monitor,
173652217195SDaniel Vetter 		 connector->num_h_tile, connector->num_v_tile,
173752217195SDaniel Vetter 		 connector->tile_h_loc, connector->tile_v_loc,
173852217195SDaniel Vetter 		 connector->tile_h_size, connector->tile_v_size);
173952217195SDaniel Vetter 
174052217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
174152217195SDaniel Vetter 	                                       &connector->tile_blob_ptr,
174252217195SDaniel Vetter 	                                       strlen(tile) + 1,
174352217195SDaniel Vetter 	                                       tile,
174452217195SDaniel Vetter 	                                       &connector->base,
174552217195SDaniel Vetter 	                                       dev->mode_config.tile_property);
174652217195SDaniel Vetter 	return ret;
174752217195SDaniel Vetter }
174897e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_tile_property);
174952217195SDaniel Vetter 
175052217195SDaniel Vetter /**
1751c555f023SDaniel Vetter  * drm_connector_update_edid_property - update the edid property of a connector
175252217195SDaniel Vetter  * @connector: drm connector
175352217195SDaniel Vetter  * @edid: new value of the edid property
175452217195SDaniel Vetter  *
175552217195SDaniel Vetter  * This function creates a new blob modeset object and assigns its id to the
175652217195SDaniel Vetter  * connector's edid property.
17572de3a078SManasi Navare  * Since we also parse tile information from EDID's displayID block, we also
17582de3a078SManasi Navare  * set the connector's tile property here. See drm_connector_set_tile_property()
17592de3a078SManasi Navare  * for more details.
176052217195SDaniel Vetter  *
176152217195SDaniel Vetter  * Returns:
176252217195SDaniel Vetter  * Zero on success, negative errno on failure.
176352217195SDaniel Vetter  */
1764c555f023SDaniel Vetter int drm_connector_update_edid_property(struct drm_connector *connector,
176552217195SDaniel Vetter 				       const struct edid *edid)
176652217195SDaniel Vetter {
176752217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
176852217195SDaniel Vetter 	size_t size = 0;
176952217195SDaniel Vetter 	int ret;
177052217195SDaniel Vetter 
177152217195SDaniel Vetter 	/* ignore requests to set edid when overridden */
177252217195SDaniel Vetter 	if (connector->override_edid)
177352217195SDaniel Vetter 		return 0;
177452217195SDaniel Vetter 
177552217195SDaniel Vetter 	if (edid)
177652217195SDaniel Vetter 		size = EDID_LENGTH * (1 + edid->extensions);
177752217195SDaniel Vetter 
1778170178feSKeith Packard 	/* Set the display info, using edid if available, otherwise
1779170178feSKeith Packard 	 * reseting the values to defaults. This duplicates the work
1780170178feSKeith Packard 	 * done in drm_add_edid_modes, but that function is not
1781170178feSKeith Packard 	 * consistently called before this one in all drivers and the
1782170178feSKeith Packard 	 * computation is cheap enough that it seems better to
1783170178feSKeith Packard 	 * duplicate it rather than attempt to ensure some arbitrary
1784170178feSKeith Packard 	 * ordering of calls.
1785170178feSKeith Packard 	 */
1786170178feSKeith Packard 	if (edid)
1787170178feSKeith Packard 		drm_add_display_info(connector, edid);
1788170178feSKeith Packard 	else
1789170178feSKeith Packard 		drm_reset_display_info(connector);
1790170178feSKeith Packard 
179166660d4cSDave Airlie 	drm_object_property_set_value(&connector->base,
179266660d4cSDave Airlie 				      dev->mode_config.non_desktop_property,
179366660d4cSDave Airlie 				      connector->display_info.non_desktop);
179466660d4cSDave Airlie 
179552217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
179652217195SDaniel Vetter 					       &connector->edid_blob_ptr,
179752217195SDaniel Vetter 	                                       size,
179852217195SDaniel Vetter 	                                       edid,
179952217195SDaniel Vetter 	                                       &connector->base,
180052217195SDaniel Vetter 	                                       dev->mode_config.edid_property);
18012de3a078SManasi Navare 	if (ret)
180252217195SDaniel Vetter 		return ret;
18032de3a078SManasi Navare 	return drm_connector_set_tile_property(connector);
180452217195SDaniel Vetter }
1805c555f023SDaniel Vetter EXPORT_SYMBOL(drm_connector_update_edid_property);
180652217195SDaniel Vetter 
180740ee6fbeSManasi Navare /**
180897e14fbeSDaniel Vetter  * drm_connector_set_link_status_property - Set link status property of a connector
180940ee6fbeSManasi Navare  * @connector: drm connector
181040ee6fbeSManasi Navare  * @link_status: new value of link status property (0: Good, 1: Bad)
181140ee6fbeSManasi Navare  *
181240ee6fbeSManasi Navare  * In usual working scenario, this link status property will always be set to
181340ee6fbeSManasi Navare  * "GOOD". If something fails during or after a mode set, the kernel driver
181440ee6fbeSManasi Navare  * may set this link status property to "BAD". The caller then needs to send a
181540ee6fbeSManasi Navare  * hotplug uevent for userspace to re-check the valid modes through
181640ee6fbeSManasi Navare  * GET_CONNECTOR_IOCTL and retry modeset.
181740ee6fbeSManasi Navare  *
181840ee6fbeSManasi Navare  * Note: Drivers cannot rely on userspace to support this property and
181940ee6fbeSManasi Navare  * issue a modeset. As such, they may choose to handle issues (like
182040ee6fbeSManasi Navare  * re-training a link) without userspace's intervention.
182140ee6fbeSManasi Navare  *
182240ee6fbeSManasi Navare  * The reason for adding this property is to handle link training failures, but
182340ee6fbeSManasi Navare  * it is not limited to DP or link training. For example, if we implement
182440ee6fbeSManasi Navare  * asynchronous setcrtc, this property can be used to report any failures in that.
182540ee6fbeSManasi Navare  */
182697e14fbeSDaniel Vetter void drm_connector_set_link_status_property(struct drm_connector *connector,
182740ee6fbeSManasi Navare 					    uint64_t link_status)
182840ee6fbeSManasi Navare {
182940ee6fbeSManasi Navare 	struct drm_device *dev = connector->dev;
183040ee6fbeSManasi Navare 
183140ee6fbeSManasi Navare 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
183240ee6fbeSManasi Navare 	connector->state->link_status = link_status;
183340ee6fbeSManasi Navare 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
183440ee6fbeSManasi Navare }
183597e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_link_status_property);
183640ee6fbeSManasi Navare 
18378d70f395SHans de Goede /**
183847e22ff1SRadhakrishna Sripada  * drm_connector_attach_max_bpc_property - attach "max bpc" property
183947e22ff1SRadhakrishna Sripada  * @connector: connector to attach max bpc property on.
184047e22ff1SRadhakrishna Sripada  * @min: The minimum bit depth supported by the connector.
184147e22ff1SRadhakrishna Sripada  * @max: The maximum bit depth supported by the connector.
184247e22ff1SRadhakrishna Sripada  *
184347e22ff1SRadhakrishna Sripada  * This is used to add support for limiting the bit depth on a connector.
184447e22ff1SRadhakrishna Sripada  *
184547e22ff1SRadhakrishna Sripada  * Returns:
184647e22ff1SRadhakrishna Sripada  * Zero on success, negative errno on failure.
184747e22ff1SRadhakrishna Sripada  */
184847e22ff1SRadhakrishna Sripada int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
184947e22ff1SRadhakrishna Sripada 					  int min, int max)
185047e22ff1SRadhakrishna Sripada {
185147e22ff1SRadhakrishna Sripada 	struct drm_device *dev = connector->dev;
185247e22ff1SRadhakrishna Sripada 	struct drm_property *prop;
185347e22ff1SRadhakrishna Sripada 
185447e22ff1SRadhakrishna Sripada 	prop = connector->max_bpc_property;
185547e22ff1SRadhakrishna Sripada 	if (!prop) {
185647e22ff1SRadhakrishna Sripada 		prop = drm_property_create_range(dev, 0, "max bpc", min, max);
185747e22ff1SRadhakrishna Sripada 		if (!prop)
185847e22ff1SRadhakrishna Sripada 			return -ENOMEM;
185947e22ff1SRadhakrishna Sripada 
186047e22ff1SRadhakrishna Sripada 		connector->max_bpc_property = prop;
186147e22ff1SRadhakrishna Sripada 	}
186247e22ff1SRadhakrishna Sripada 
186347e22ff1SRadhakrishna Sripada 	drm_object_attach_property(&connector->base, prop, max);
186447e22ff1SRadhakrishna Sripada 	connector->state->max_requested_bpc = max;
186547e22ff1SRadhakrishna Sripada 	connector->state->max_bpc = max;
186647e22ff1SRadhakrishna Sripada 
186747e22ff1SRadhakrishna Sripada 	return 0;
186847e22ff1SRadhakrishna Sripada }
186947e22ff1SRadhakrishna Sripada EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
187047e22ff1SRadhakrishna Sripada 
187147e22ff1SRadhakrishna Sripada /**
1872ba1b0f6cSNicholas Kazlauskas  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
1873ba1b0f6cSNicholas Kazlauskas  * capable property for a connector
1874ba1b0f6cSNicholas Kazlauskas  * @connector: drm connector
1875ba1b0f6cSNicholas Kazlauskas  * @capable: True if the connector is variable refresh rate capable
1876ba1b0f6cSNicholas Kazlauskas  *
1877ba1b0f6cSNicholas Kazlauskas  * Should be used by atomic drivers to update the indicated support for
1878ba1b0f6cSNicholas Kazlauskas  * variable refresh rate over a connector.
1879ba1b0f6cSNicholas Kazlauskas  */
1880ba1b0f6cSNicholas Kazlauskas void drm_connector_set_vrr_capable_property(
1881ba1b0f6cSNicholas Kazlauskas 		struct drm_connector *connector, bool capable)
1882ba1b0f6cSNicholas Kazlauskas {
1883ba1b0f6cSNicholas Kazlauskas 	drm_object_property_set_value(&connector->base,
1884ba1b0f6cSNicholas Kazlauskas 				      connector->vrr_capable_property,
1885ba1b0f6cSNicholas Kazlauskas 				      capable);
1886ba1b0f6cSNicholas Kazlauskas }
1887ba1b0f6cSNicholas Kazlauskas EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
1888ba1b0f6cSNicholas Kazlauskas 
1889ba1b0f6cSNicholas Kazlauskas /**
18908d70f395SHans de Goede  * drm_connector_init_panel_orientation_property -
18918d70f395SHans de Goede  *	initialize the connecters panel_orientation property
18928d70f395SHans de Goede  * @connector: connector for which to init the panel-orientation property.
18938d70f395SHans de Goede  * @width: width in pixels of the panel, used for panel quirk detection
18948d70f395SHans de Goede  * @height: height in pixels of the panel, used for panel quirk detection
18958d70f395SHans de Goede  *
18968d70f395SHans de Goede  * This function should only be called for built-in panels, after setting
18978d70f395SHans de Goede  * connector->display_info.panel_orientation first (if known).
18988d70f395SHans de Goede  *
18998d70f395SHans de Goede  * This function will check for platform specific (e.g. DMI based) quirks
19008d70f395SHans de Goede  * overriding display_info.panel_orientation first, then if panel_orientation
19018d70f395SHans de Goede  * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
19028d70f395SHans de Goede  * "panel orientation" property to the connector.
19038d70f395SHans de Goede  *
19048d70f395SHans de Goede  * Returns:
19058d70f395SHans de Goede  * Zero on success, negative errno on failure.
19068d70f395SHans de Goede  */
19078d70f395SHans de Goede int drm_connector_init_panel_orientation_property(
19088d70f395SHans de Goede 	struct drm_connector *connector, int width, int height)
19098d70f395SHans de Goede {
19108d70f395SHans de Goede 	struct drm_device *dev = connector->dev;
19118d70f395SHans de Goede 	struct drm_display_info *info = &connector->display_info;
19128d70f395SHans de Goede 	struct drm_property *prop;
19138d70f395SHans de Goede 	int orientation_quirk;
19148d70f395SHans de Goede 
19158d70f395SHans de Goede 	orientation_quirk = drm_get_panel_orientation_quirk(width, height);
19168d70f395SHans de Goede 	if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
19178d70f395SHans de Goede 		info->panel_orientation = orientation_quirk;
19188d70f395SHans de Goede 
19198d70f395SHans de Goede 	if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
19208d70f395SHans de Goede 		return 0;
19218d70f395SHans de Goede 
19228d70f395SHans de Goede 	prop = dev->mode_config.panel_orientation_property;
19238d70f395SHans de Goede 	if (!prop) {
19248d70f395SHans de Goede 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
19258d70f395SHans de Goede 				"panel orientation",
19268d70f395SHans de Goede 				drm_panel_orientation_enum_list,
19278d70f395SHans de Goede 				ARRAY_SIZE(drm_panel_orientation_enum_list));
19288d70f395SHans de Goede 		if (!prop)
19298d70f395SHans de Goede 			return -ENOMEM;
19308d70f395SHans de Goede 
19318d70f395SHans de Goede 		dev->mode_config.panel_orientation_property = prop;
19328d70f395SHans de Goede 	}
19338d70f395SHans de Goede 
19348d70f395SHans de Goede 	drm_object_attach_property(&connector->base, prop,
19358d70f395SHans de Goede 				   info->panel_orientation);
19368d70f395SHans de Goede 	return 0;
19378d70f395SHans de Goede }
19388d70f395SHans de Goede EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
19398d70f395SHans de Goede 
194097e14fbeSDaniel Vetter int drm_connector_set_obj_prop(struct drm_mode_object *obj,
194152217195SDaniel Vetter 				    struct drm_property *property,
194252217195SDaniel Vetter 				    uint64_t value)
194352217195SDaniel Vetter {
194452217195SDaniel Vetter 	int ret = -EINVAL;
194552217195SDaniel Vetter 	struct drm_connector *connector = obj_to_connector(obj);
194652217195SDaniel Vetter 
194752217195SDaniel Vetter 	/* Do DPMS ourselves */
194852217195SDaniel Vetter 	if (property == connector->dev->mode_config.dpms_property) {
194952217195SDaniel Vetter 		ret = (*connector->funcs->dpms)(connector, (int)value);
195052217195SDaniel Vetter 	} else if (connector->funcs->set_property)
195152217195SDaniel Vetter 		ret = connector->funcs->set_property(connector, property, value);
195252217195SDaniel Vetter 
1953144a7999SDaniel Vetter 	if (!ret)
195452217195SDaniel Vetter 		drm_object_property_set_value(&connector->base, property, value);
195552217195SDaniel Vetter 	return ret;
195652217195SDaniel Vetter }
195752217195SDaniel Vetter 
195897e14fbeSDaniel Vetter int drm_connector_property_set_ioctl(struct drm_device *dev,
195952217195SDaniel Vetter 				     void *data, struct drm_file *file_priv)
196052217195SDaniel Vetter {
196152217195SDaniel Vetter 	struct drm_mode_connector_set_property *conn_set_prop = data;
196252217195SDaniel Vetter 	struct drm_mode_obj_set_property obj_set_prop = {
196352217195SDaniel Vetter 		.value = conn_set_prop->value,
196452217195SDaniel Vetter 		.prop_id = conn_set_prop->prop_id,
196552217195SDaniel Vetter 		.obj_id = conn_set_prop->connector_id,
196652217195SDaniel Vetter 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
196752217195SDaniel Vetter 	};
196852217195SDaniel Vetter 
196952217195SDaniel Vetter 	/* It does all the locking and checking we need */
197052217195SDaniel Vetter 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
197152217195SDaniel Vetter }
197252217195SDaniel Vetter 
197352217195SDaniel Vetter static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
197452217195SDaniel Vetter {
197552217195SDaniel Vetter 	/* For atomic drivers only state objects are synchronously updated and
197652217195SDaniel Vetter 	 * protected by modeset locks, so check those first. */
197752217195SDaniel Vetter 	if (connector->state)
197852217195SDaniel Vetter 		return connector->state->best_encoder;
197952217195SDaniel Vetter 	return connector->encoder;
198052217195SDaniel Vetter }
198152217195SDaniel Vetter 
1982c3ff0cdbSAnkit Nautiyal static bool
1983c3ff0cdbSAnkit Nautiyal drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1984c3ff0cdbSAnkit Nautiyal 			     const struct list_head *export_list,
198552217195SDaniel Vetter 			     const struct drm_file *file_priv)
198652217195SDaniel Vetter {
198752217195SDaniel Vetter 	/*
198852217195SDaniel Vetter 	 * If user-space hasn't configured the driver to expose the stereo 3D
198952217195SDaniel Vetter 	 * modes, don't expose them.
199052217195SDaniel Vetter 	 */
199152217195SDaniel Vetter 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
199252217195SDaniel Vetter 		return false;
1993c3ff0cdbSAnkit Nautiyal 	/*
1994c3ff0cdbSAnkit Nautiyal 	 * If user-space hasn't configured the driver to expose the modes
1995c3ff0cdbSAnkit Nautiyal 	 * with aspect-ratio, don't expose them. However if such a mode
1996c3ff0cdbSAnkit Nautiyal 	 * is unique, let it be exposed, but reset the aspect-ratio flags
1997c3ff0cdbSAnkit Nautiyal 	 * while preparing the list of user-modes.
1998c3ff0cdbSAnkit Nautiyal 	 */
1999c3ff0cdbSAnkit Nautiyal 	if (!file_priv->aspect_ratio_allowed) {
2000c3ff0cdbSAnkit Nautiyal 		struct drm_display_mode *mode_itr;
2001c3ff0cdbSAnkit Nautiyal 
2002c3ff0cdbSAnkit Nautiyal 		list_for_each_entry(mode_itr, export_list, export_head)
2003c3ff0cdbSAnkit Nautiyal 			if (drm_mode_match(mode_itr, mode,
2004c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_TIMINGS |
2005c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_CLOCK |
2006c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_FLAGS |
2007c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_3D_FLAGS))
2008c3ff0cdbSAnkit Nautiyal 				return false;
2009c3ff0cdbSAnkit Nautiyal 	}
201052217195SDaniel Vetter 
201152217195SDaniel Vetter 	return true;
201252217195SDaniel Vetter }
201352217195SDaniel Vetter 
201452217195SDaniel Vetter int drm_mode_getconnector(struct drm_device *dev, void *data,
201552217195SDaniel Vetter 			  struct drm_file *file_priv)
201652217195SDaniel Vetter {
201752217195SDaniel Vetter 	struct drm_mode_get_connector *out_resp = data;
201852217195SDaniel Vetter 	struct drm_connector *connector;
201952217195SDaniel Vetter 	struct drm_encoder *encoder;
202052217195SDaniel Vetter 	struct drm_display_mode *mode;
202152217195SDaniel Vetter 	int mode_count = 0;
202252217195SDaniel Vetter 	int encoders_count = 0;
202352217195SDaniel Vetter 	int ret = 0;
202452217195SDaniel Vetter 	int copied = 0;
202552217195SDaniel Vetter 	int i;
202652217195SDaniel Vetter 	struct drm_mode_modeinfo u_mode;
202752217195SDaniel Vetter 	struct drm_mode_modeinfo __user *mode_ptr;
202852217195SDaniel Vetter 	uint32_t __user *encoder_ptr;
2029c3ff0cdbSAnkit Nautiyal 	LIST_HEAD(export_list);
203052217195SDaniel Vetter 
203152217195SDaniel Vetter 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
203269fdf420SChris Wilson 		return -EOPNOTSUPP;
203352217195SDaniel Vetter 
203452217195SDaniel Vetter 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
203552217195SDaniel Vetter 
2036418da172SKeith Packard 	connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
203791eefc05SDaniel Vetter 	if (!connector)
203891eefc05SDaniel Vetter 		return -ENOENT;
203952217195SDaniel Vetter 
204083aefbb8SVille Syrjälä 	drm_connector_for_each_possible_encoder(connector, encoder, i)
204191eefc05SDaniel Vetter 		encoders_count++;
204291eefc05SDaniel Vetter 
204391eefc05SDaniel Vetter 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
204491eefc05SDaniel Vetter 		copied = 0;
204591eefc05SDaniel Vetter 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
204683aefbb8SVille Syrjälä 
204783aefbb8SVille Syrjälä 		drm_connector_for_each_possible_encoder(connector, encoder, i) {
204883aefbb8SVille Syrjälä 			if (put_user(encoder->base.id, encoder_ptr + copied)) {
204991eefc05SDaniel Vetter 				ret = -EFAULT;
2050e94ac351SDaniel Vetter 				goto out;
205191eefc05SDaniel Vetter 			}
205291eefc05SDaniel Vetter 			copied++;
205391eefc05SDaniel Vetter 		}
205491eefc05SDaniel Vetter 	}
205591eefc05SDaniel Vetter 	out_resp->count_encoders = encoders_count;
205691eefc05SDaniel Vetter 
205791eefc05SDaniel Vetter 	out_resp->connector_id = connector->base.id;
205891eefc05SDaniel Vetter 	out_resp->connector_type = connector->connector_type;
205991eefc05SDaniel Vetter 	out_resp->connector_type_id = connector->connector_type_id;
206091eefc05SDaniel Vetter 
206191eefc05SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
206291eefc05SDaniel Vetter 	if (out_resp->count_modes == 0) {
206391eefc05SDaniel Vetter 		connector->funcs->fill_modes(connector,
206491eefc05SDaniel Vetter 					     dev->mode_config.max_width,
206591eefc05SDaniel Vetter 					     dev->mode_config.max_height);
206691eefc05SDaniel Vetter 	}
206791eefc05SDaniel Vetter 
206891eefc05SDaniel Vetter 	out_resp->mm_width = connector->display_info.width_mm;
206991eefc05SDaniel Vetter 	out_resp->mm_height = connector->display_info.height_mm;
207091eefc05SDaniel Vetter 	out_resp->subpixel = connector->display_info.subpixel_order;
207191eefc05SDaniel Vetter 	out_resp->connection = connector->status;
207291eefc05SDaniel Vetter 
207391eefc05SDaniel Vetter 	/* delayed so we get modes regardless of pre-fill_modes state */
207491eefc05SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
2075c3ff0cdbSAnkit Nautiyal 		if (drm_mode_expose_to_userspace(mode, &export_list,
2076c3ff0cdbSAnkit Nautiyal 						 file_priv)) {
2077c3ff0cdbSAnkit Nautiyal 			list_add_tail(&mode->export_head, &export_list);
207891eefc05SDaniel Vetter 			mode_count++;
2079c3ff0cdbSAnkit Nautiyal 		}
208091eefc05SDaniel Vetter 
208152217195SDaniel Vetter 	/*
208252217195SDaniel Vetter 	 * This ioctl is called twice, once to determine how much space is
208352217195SDaniel Vetter 	 * needed, and the 2nd time to fill it.
2084c3ff0cdbSAnkit Nautiyal 	 * The modes that need to be exposed to the user are maintained in the
2085c3ff0cdbSAnkit Nautiyal 	 * 'export_list'. When the ioctl is called first time to determine the,
2086c3ff0cdbSAnkit Nautiyal 	 * space, the export_list gets filled, to find the no.of modes. In the
2087c3ff0cdbSAnkit Nautiyal 	 * 2nd time, the user modes are filled, one by one from the export_list.
208852217195SDaniel Vetter 	 */
208952217195SDaniel Vetter 	if ((out_resp->count_modes >= mode_count) && mode_count) {
209052217195SDaniel Vetter 		copied = 0;
209152217195SDaniel Vetter 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2092c3ff0cdbSAnkit Nautiyal 		list_for_each_entry(mode, &export_list, export_head) {
209352217195SDaniel Vetter 			drm_mode_convert_to_umode(&u_mode, mode);
2094c3ff0cdbSAnkit Nautiyal 			/*
2095c3ff0cdbSAnkit Nautiyal 			 * Reset aspect ratio flags of user-mode, if modes with
2096c3ff0cdbSAnkit Nautiyal 			 * aspect-ratio are not supported.
2097c3ff0cdbSAnkit Nautiyal 			 */
2098c3ff0cdbSAnkit Nautiyal 			if (!file_priv->aspect_ratio_allowed)
2099c3ff0cdbSAnkit Nautiyal 				u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
210052217195SDaniel Vetter 			if (copy_to_user(mode_ptr + copied,
210152217195SDaniel Vetter 					 &u_mode, sizeof(u_mode))) {
210252217195SDaniel Vetter 				ret = -EFAULT;
2103e94ac351SDaniel Vetter 				mutex_unlock(&dev->mode_config.mutex);
2104e94ac351SDaniel Vetter 
210552217195SDaniel Vetter 				goto out;
210652217195SDaniel Vetter 			}
210752217195SDaniel Vetter 			copied++;
210852217195SDaniel Vetter 		}
210952217195SDaniel Vetter 	}
211052217195SDaniel Vetter 	out_resp->count_modes = mode_count;
211152217195SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
2112e94ac351SDaniel Vetter 
2113e94ac351SDaniel Vetter 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2114e94ac351SDaniel Vetter 	encoder = drm_connector_get_encoder(connector);
2115e94ac351SDaniel Vetter 	if (encoder)
2116e94ac351SDaniel Vetter 		out_resp->encoder_id = encoder->base.id;
2117e94ac351SDaniel Vetter 	else
2118e94ac351SDaniel Vetter 		out_resp->encoder_id = 0;
2119e94ac351SDaniel Vetter 
2120e94ac351SDaniel Vetter 	/* Only grab properties after probing, to make sure EDID and other
2121e94ac351SDaniel Vetter 	 * properties reflect the latest status. */
2122e94ac351SDaniel Vetter 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
2123e94ac351SDaniel Vetter 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2124e94ac351SDaniel Vetter 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2125e94ac351SDaniel Vetter 			&out_resp->count_props);
2126e94ac351SDaniel Vetter 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2127e94ac351SDaniel Vetter 
2128e94ac351SDaniel Vetter out:
2129ad093607SThierry Reding 	drm_connector_put(connector);
213052217195SDaniel Vetter 
213152217195SDaniel Vetter 	return ret;
213252217195SDaniel Vetter }
213352217195SDaniel Vetter 
21349498c19bSDaniel Vetter 
21359498c19bSDaniel Vetter /**
21369498c19bSDaniel Vetter  * DOC: Tile group
21379498c19bSDaniel Vetter  *
21389498c19bSDaniel Vetter  * Tile groups are used to represent tiled monitors with a unique integer
21399498c19bSDaniel Vetter  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
21409498c19bSDaniel Vetter  * we store this in a tile group, so we have a common identifier for all tiles
21419498c19bSDaniel Vetter  * in a monitor group. The property is called "TILE". Drivers can manage tile
21429498c19bSDaniel Vetter  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
21439498c19bSDaniel Vetter  * drm_mode_get_tile_group(). But this is only needed for internal panels where
21449498c19bSDaniel Vetter  * the tile group information is exposed through a non-standard way.
21459498c19bSDaniel Vetter  */
21469498c19bSDaniel Vetter 
21479498c19bSDaniel Vetter static void drm_tile_group_free(struct kref *kref)
21489498c19bSDaniel Vetter {
21499498c19bSDaniel Vetter 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
21509498c19bSDaniel Vetter 	struct drm_device *dev = tg->dev;
21519498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
21529498c19bSDaniel Vetter 	idr_remove(&dev->mode_config.tile_idr, tg->id);
21539498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
21549498c19bSDaniel Vetter 	kfree(tg);
21559498c19bSDaniel Vetter }
21569498c19bSDaniel Vetter 
21579498c19bSDaniel Vetter /**
21589498c19bSDaniel Vetter  * drm_mode_put_tile_group - drop a reference to a tile group.
21599498c19bSDaniel Vetter  * @dev: DRM device
21609498c19bSDaniel Vetter  * @tg: tile group to drop reference to.
21619498c19bSDaniel Vetter  *
21629498c19bSDaniel Vetter  * drop reference to tile group and free if 0.
21639498c19bSDaniel Vetter  */
21649498c19bSDaniel Vetter void drm_mode_put_tile_group(struct drm_device *dev,
21659498c19bSDaniel Vetter 			     struct drm_tile_group *tg)
21669498c19bSDaniel Vetter {
21679498c19bSDaniel Vetter 	kref_put(&tg->refcount, drm_tile_group_free);
21689498c19bSDaniel Vetter }
21699498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_put_tile_group);
21709498c19bSDaniel Vetter 
21719498c19bSDaniel Vetter /**
21729498c19bSDaniel Vetter  * drm_mode_get_tile_group - get a reference to an existing tile group
21739498c19bSDaniel Vetter  * @dev: DRM device
21749498c19bSDaniel Vetter  * @topology: 8-bytes unique per monitor.
21759498c19bSDaniel Vetter  *
21769498c19bSDaniel Vetter  * Use the unique bytes to get a reference to an existing tile group.
21779498c19bSDaniel Vetter  *
21789498c19bSDaniel Vetter  * RETURNS:
21799498c19bSDaniel Vetter  * tile group or NULL if not found.
21809498c19bSDaniel Vetter  */
21819498c19bSDaniel Vetter struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
21829498c19bSDaniel Vetter 					       char topology[8])
21839498c19bSDaniel Vetter {
21849498c19bSDaniel Vetter 	struct drm_tile_group *tg;
21859498c19bSDaniel Vetter 	int id;
21869498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
21879498c19bSDaniel Vetter 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
21889498c19bSDaniel Vetter 		if (!memcmp(tg->group_data, topology, 8)) {
21899498c19bSDaniel Vetter 			if (!kref_get_unless_zero(&tg->refcount))
21909498c19bSDaniel Vetter 				tg = NULL;
21919498c19bSDaniel Vetter 			mutex_unlock(&dev->mode_config.idr_mutex);
21929498c19bSDaniel Vetter 			return tg;
21939498c19bSDaniel Vetter 		}
21949498c19bSDaniel Vetter 	}
21959498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
21969498c19bSDaniel Vetter 	return NULL;
21979498c19bSDaniel Vetter }
21989498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_get_tile_group);
21999498c19bSDaniel Vetter 
22009498c19bSDaniel Vetter /**
22019498c19bSDaniel Vetter  * drm_mode_create_tile_group - create a tile group from a displayid description
22029498c19bSDaniel Vetter  * @dev: DRM device
22039498c19bSDaniel Vetter  * @topology: 8-bytes unique per monitor.
22049498c19bSDaniel Vetter  *
22059498c19bSDaniel Vetter  * Create a tile group for the unique monitor, and get a unique
22069498c19bSDaniel Vetter  * identifier for the tile group.
22079498c19bSDaniel Vetter  *
22089498c19bSDaniel Vetter  * RETURNS:
2209705c8160SDan Carpenter  * new tile group or NULL.
22109498c19bSDaniel Vetter  */
22119498c19bSDaniel Vetter struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
22129498c19bSDaniel Vetter 						  char topology[8])
22139498c19bSDaniel Vetter {
22149498c19bSDaniel Vetter 	struct drm_tile_group *tg;
22159498c19bSDaniel Vetter 	int ret;
22169498c19bSDaniel Vetter 
22179498c19bSDaniel Vetter 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
22189498c19bSDaniel Vetter 	if (!tg)
2219705c8160SDan Carpenter 		return NULL;
22209498c19bSDaniel Vetter 
22219498c19bSDaniel Vetter 	kref_init(&tg->refcount);
22229498c19bSDaniel Vetter 	memcpy(tg->group_data, topology, 8);
22239498c19bSDaniel Vetter 	tg->dev = dev;
22249498c19bSDaniel Vetter 
22259498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
22269498c19bSDaniel Vetter 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
22279498c19bSDaniel Vetter 	if (ret >= 0) {
22289498c19bSDaniel Vetter 		tg->id = ret;
22299498c19bSDaniel Vetter 	} else {
22309498c19bSDaniel Vetter 		kfree(tg);
2231705c8160SDan Carpenter 		tg = NULL;
22329498c19bSDaniel Vetter 	}
22339498c19bSDaniel Vetter 
22349498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
22359498c19bSDaniel Vetter 	return tg;
22369498c19bSDaniel Vetter }
22379498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_create_tile_group);
2238