xref: /linux/drivers/gpu/drm/drm_connector.c (revision 62afb4ad425af2bc6ac6ff6d697825ae47c25211)
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" },
95fc06bf1dSNoralf Trønnes 	{ DRM_MODE_CONNECTOR_SPI, "SPI" },
9652217195SDaniel Vetter };
9752217195SDaniel Vetter 
9852217195SDaniel Vetter void drm_connector_ida_init(void)
9952217195SDaniel Vetter {
10052217195SDaniel Vetter 	int i;
10152217195SDaniel Vetter 
10252217195SDaniel Vetter 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
10352217195SDaniel Vetter 		ida_init(&drm_connector_enum_list[i].ida);
10452217195SDaniel Vetter }
10552217195SDaniel Vetter 
10652217195SDaniel Vetter void drm_connector_ida_destroy(void)
10752217195SDaniel Vetter {
10852217195SDaniel Vetter 	int i;
10952217195SDaniel Vetter 
11052217195SDaniel Vetter 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
11152217195SDaniel Vetter 		ida_destroy(&drm_connector_enum_list[i].ida);
11252217195SDaniel Vetter }
11352217195SDaniel Vetter 
11452217195SDaniel Vetter /**
11552217195SDaniel Vetter  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
11652217195SDaniel Vetter  * @connector: connector to quwery
11752217195SDaniel Vetter  *
118ae2a6da8SDaniel Vetter  * The kernel supports per-connector configuration of its consoles through
11952217195SDaniel Vetter  * use of the video= parameter. This function parses that option and
12052217195SDaniel Vetter  * extracts the user's specified mode (or enable/disable status) for a
12152217195SDaniel Vetter  * particular connector. This is typically only used during the early fbdev
12252217195SDaniel Vetter  * setup.
12352217195SDaniel Vetter  */
12452217195SDaniel Vetter static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
12552217195SDaniel Vetter {
12652217195SDaniel Vetter 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
12752217195SDaniel Vetter 	char *option = NULL;
12852217195SDaniel Vetter 
12952217195SDaniel Vetter 	if (fb_get_options(connector->name, &option))
13052217195SDaniel Vetter 		return;
13152217195SDaniel Vetter 
13252217195SDaniel Vetter 	if (!drm_mode_parse_command_line_for_connector(option,
13352217195SDaniel Vetter 						       connector,
13452217195SDaniel Vetter 						       mode))
13552217195SDaniel Vetter 		return;
13652217195SDaniel Vetter 
13752217195SDaniel Vetter 	if (mode->force) {
1386140cf20SJani Nikula 		DRM_INFO("forcing %s connector %s\n", connector->name,
1396140cf20SJani Nikula 			 drm_get_connector_force_name(mode->force));
14052217195SDaniel Vetter 		connector->force = mode->force;
14152217195SDaniel Vetter 	}
14252217195SDaniel Vetter 
1433aeeb13dSMaxime Ripard 	DRM_DEBUG_KMS("cmdline mode for connector %s %s %dx%d@%dHz%s%s%s\n",
14450b0946dSMaxime Ripard 		      connector->name, mode->name,
14552217195SDaniel Vetter 		      mode->xres, mode->yres,
14652217195SDaniel Vetter 		      mode->refresh_specified ? mode->refresh : 60,
14752217195SDaniel Vetter 		      mode->rb ? " reduced blanking" : "",
14852217195SDaniel Vetter 		      mode->margins ? " with margins" : "",
14952217195SDaniel Vetter 		      mode->interlace ?  " interlaced" : "");
15052217195SDaniel Vetter }
15152217195SDaniel Vetter 
15252217195SDaniel Vetter static void drm_connector_free(struct kref *kref)
15352217195SDaniel Vetter {
15452217195SDaniel Vetter 	struct drm_connector *connector =
15552217195SDaniel Vetter 		container_of(kref, struct drm_connector, base.refcount);
15652217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
15752217195SDaniel Vetter 
15852217195SDaniel Vetter 	drm_mode_object_unregister(dev, &connector->base);
15952217195SDaniel Vetter 	connector->funcs->destroy(connector);
16052217195SDaniel Vetter }
16152217195SDaniel Vetter 
162ea497bb9SDaniel Vetter void drm_connector_free_work_fn(struct work_struct *work)
163a703c550SDaniel Vetter {
164ea497bb9SDaniel Vetter 	struct drm_connector *connector, *n;
165ea497bb9SDaniel Vetter 	struct drm_device *dev =
166ea497bb9SDaniel Vetter 		container_of(work, struct drm_device, mode_config.connector_free_work);
167ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &dev->mode_config;
168ea497bb9SDaniel Vetter 	unsigned long flags;
169ea497bb9SDaniel Vetter 	struct llist_node *freed;
170a703c550SDaniel Vetter 
171ea497bb9SDaniel Vetter 	spin_lock_irqsave(&config->connector_list_lock, flags);
172ea497bb9SDaniel Vetter 	freed = llist_del_all(&config->connector_free_list);
173ea497bb9SDaniel Vetter 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
174ea497bb9SDaniel Vetter 
175ea497bb9SDaniel Vetter 	llist_for_each_entry_safe(connector, n, freed, free_node) {
176a703c550SDaniel Vetter 		drm_mode_object_unregister(dev, &connector->base);
177a703c550SDaniel Vetter 		connector->funcs->destroy(connector);
178a703c550SDaniel Vetter 	}
179ea497bb9SDaniel Vetter }
180a703c550SDaniel Vetter 
18152217195SDaniel Vetter /**
18252217195SDaniel Vetter  * drm_connector_init - Init a preallocated connector
18352217195SDaniel Vetter  * @dev: DRM device
18452217195SDaniel Vetter  * @connector: the connector to init
18552217195SDaniel Vetter  * @funcs: callbacks for this connector
18652217195SDaniel Vetter  * @connector_type: user visible type of the connector
18752217195SDaniel Vetter  *
18852217195SDaniel Vetter  * Initialises a preallocated connector. Connectors should be
18952217195SDaniel Vetter  * subclassed as part of driver connector objects.
19052217195SDaniel Vetter  *
19152217195SDaniel Vetter  * Returns:
19252217195SDaniel Vetter  * Zero on success, error code on failure.
19352217195SDaniel Vetter  */
19452217195SDaniel Vetter int drm_connector_init(struct drm_device *dev,
19552217195SDaniel Vetter 		       struct drm_connector *connector,
19652217195SDaniel Vetter 		       const struct drm_connector_funcs *funcs,
19752217195SDaniel Vetter 		       int connector_type)
19852217195SDaniel Vetter {
19952217195SDaniel Vetter 	struct drm_mode_config *config = &dev->mode_config;
20052217195SDaniel Vetter 	int ret;
20152217195SDaniel Vetter 	struct ida *connector_ida =
20252217195SDaniel Vetter 		&drm_connector_enum_list[connector_type].ida;
20352217195SDaniel Vetter 
204ba1f665fSHaneen Mohammed 	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
205ba1f665fSHaneen Mohammed 		(!funcs->atomic_destroy_state ||
206ba1f665fSHaneen Mohammed 		 !funcs->atomic_duplicate_state));
207ba1f665fSHaneen Mohammed 
2082135ea7aSThierry Reding 	ret = __drm_mode_object_add(dev, &connector->base,
20952217195SDaniel Vetter 				    DRM_MODE_OBJECT_CONNECTOR,
21052217195SDaniel Vetter 				    false, drm_connector_free);
21152217195SDaniel Vetter 	if (ret)
212613051daSDaniel Vetter 		return ret;
21352217195SDaniel Vetter 
21452217195SDaniel Vetter 	connector->base.properties = &connector->properties;
21552217195SDaniel Vetter 	connector->dev = dev;
21652217195SDaniel Vetter 	connector->funcs = funcs;
21752217195SDaniel Vetter 
2182a8d3eacSVille Syrjälä 	/* connector index is used with 32bit bitmasks */
2192a8d3eacSVille Syrjälä 	ret = ida_simple_get(&config->connector_ida, 0, 32, GFP_KERNEL);
2202a8d3eacSVille Syrjälä 	if (ret < 0) {
2212a8d3eacSVille Syrjälä 		DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
2222a8d3eacSVille Syrjälä 			      drm_connector_enum_list[connector_type].name,
2232a8d3eacSVille Syrjälä 			      ret);
22452217195SDaniel Vetter 		goto out_put;
2252a8d3eacSVille Syrjälä 	}
22652217195SDaniel Vetter 	connector->index = ret;
22752217195SDaniel Vetter 	ret = 0;
22852217195SDaniel Vetter 
22952217195SDaniel Vetter 	connector->connector_type = connector_type;
23052217195SDaniel Vetter 	connector->connector_type_id =
23152217195SDaniel Vetter 		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
23252217195SDaniel Vetter 	if (connector->connector_type_id < 0) {
23352217195SDaniel Vetter 		ret = connector->connector_type_id;
23452217195SDaniel Vetter 		goto out_put_id;
23552217195SDaniel Vetter 	}
23652217195SDaniel Vetter 	connector->name =
23752217195SDaniel Vetter 		kasprintf(GFP_KERNEL, "%s-%d",
23852217195SDaniel Vetter 			  drm_connector_enum_list[connector_type].name,
23952217195SDaniel Vetter 			  connector->connector_type_id);
24052217195SDaniel Vetter 	if (!connector->name) {
24152217195SDaniel Vetter 		ret = -ENOMEM;
24252217195SDaniel Vetter 		goto out_put_type_id;
24352217195SDaniel Vetter 	}
24452217195SDaniel Vetter 
24552217195SDaniel Vetter 	INIT_LIST_HEAD(&connector->probed_modes);
24652217195SDaniel Vetter 	INIT_LIST_HEAD(&connector->modes);
247e73ab00eSDaniel Vetter 	mutex_init(&connector->mutex);
24852217195SDaniel Vetter 	connector->edid_blob_ptr = NULL;
2492de3a078SManasi Navare 	connector->tile_blob_ptr = NULL;
25052217195SDaniel Vetter 	connector->status = connector_status_unknown;
2518d70f395SHans de Goede 	connector->display_info.panel_orientation =
2528d70f395SHans de Goede 		DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
25352217195SDaniel Vetter 
25452217195SDaniel Vetter 	drm_connector_get_cmdline_mode(connector);
25552217195SDaniel Vetter 
25652217195SDaniel Vetter 	/* We should add connectors at the end to avoid upsetting the connector
25752217195SDaniel Vetter 	 * index too much. */
258613051daSDaniel Vetter 	spin_lock_irq(&config->connector_list_lock);
25952217195SDaniel Vetter 	list_add_tail(&connector->head, &config->connector_list);
26052217195SDaniel Vetter 	config->num_connector++;
261613051daSDaniel Vetter 	spin_unlock_irq(&config->connector_list_lock);
26252217195SDaniel Vetter 
263935774cdSBrian Starkey 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
264935774cdSBrian Starkey 	    connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
2656b7e2d5cSGerd Hoffmann 		drm_connector_attach_edid_property(connector);
26652217195SDaniel Vetter 
26752217195SDaniel Vetter 	drm_object_attach_property(&connector->base,
26852217195SDaniel Vetter 				      config->dpms_property, 0);
26952217195SDaniel Vetter 
27040ee6fbeSManasi Navare 	drm_object_attach_property(&connector->base,
27140ee6fbeSManasi Navare 				   config->link_status_property,
27240ee6fbeSManasi Navare 				   0);
27340ee6fbeSManasi Navare 
27466660d4cSDave Airlie 	drm_object_attach_property(&connector->base,
27566660d4cSDave Airlie 				   config->non_desktop_property,
27666660d4cSDave Airlie 				   0);
2772de3a078SManasi Navare 	drm_object_attach_property(&connector->base,
2782de3a078SManasi Navare 				   config->tile_property,
2792de3a078SManasi Navare 				   0);
28066660d4cSDave Airlie 
28152217195SDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
28252217195SDaniel Vetter 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
28352217195SDaniel Vetter 	}
28452217195SDaniel Vetter 
28552217195SDaniel Vetter 	connector->debugfs_entry = NULL;
28652217195SDaniel Vetter out_put_type_id:
28752217195SDaniel Vetter 	if (ret)
288587680c1SChristophe JAILLET 		ida_simple_remove(connector_ida, connector->connector_type_id);
28952217195SDaniel Vetter out_put_id:
29052217195SDaniel Vetter 	if (ret)
291587680c1SChristophe JAILLET 		ida_simple_remove(&config->connector_ida, connector->index);
29252217195SDaniel Vetter out_put:
29352217195SDaniel Vetter 	if (ret)
29452217195SDaniel Vetter 		drm_mode_object_unregister(dev, &connector->base);
29552217195SDaniel Vetter 
29652217195SDaniel Vetter 	return ret;
29752217195SDaniel Vetter }
29852217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_init);
29952217195SDaniel Vetter 
30052217195SDaniel Vetter /**
301100163dfSAndrzej Pietrasiewicz  * drm_connector_init_with_ddc - Init a preallocated connector
302100163dfSAndrzej Pietrasiewicz  * @dev: DRM device
303100163dfSAndrzej Pietrasiewicz  * @connector: the connector to init
304100163dfSAndrzej Pietrasiewicz  * @funcs: callbacks for this connector
305100163dfSAndrzej Pietrasiewicz  * @connector_type: user visible type of the connector
306100163dfSAndrzej Pietrasiewicz  * @ddc: pointer to the associated ddc adapter
307100163dfSAndrzej Pietrasiewicz  *
308100163dfSAndrzej Pietrasiewicz  * Initialises a preallocated connector. Connectors should be
309100163dfSAndrzej Pietrasiewicz  * subclassed as part of driver connector objects.
310100163dfSAndrzej Pietrasiewicz  *
311100163dfSAndrzej Pietrasiewicz  * Ensures that the ddc field of the connector is correctly set.
312100163dfSAndrzej Pietrasiewicz  *
313100163dfSAndrzej Pietrasiewicz  * Returns:
314100163dfSAndrzej Pietrasiewicz  * Zero on success, error code on failure.
315100163dfSAndrzej Pietrasiewicz  */
316100163dfSAndrzej Pietrasiewicz int drm_connector_init_with_ddc(struct drm_device *dev,
317100163dfSAndrzej Pietrasiewicz 				struct drm_connector *connector,
318100163dfSAndrzej Pietrasiewicz 				const struct drm_connector_funcs *funcs,
319100163dfSAndrzej Pietrasiewicz 				int connector_type,
320100163dfSAndrzej Pietrasiewicz 				struct i2c_adapter *ddc)
321100163dfSAndrzej Pietrasiewicz {
322100163dfSAndrzej Pietrasiewicz 	int ret;
323100163dfSAndrzej Pietrasiewicz 
324100163dfSAndrzej Pietrasiewicz 	ret = drm_connector_init(dev, connector, funcs, connector_type);
325100163dfSAndrzej Pietrasiewicz 	if (ret)
326100163dfSAndrzej Pietrasiewicz 		return ret;
327100163dfSAndrzej Pietrasiewicz 
328100163dfSAndrzej Pietrasiewicz 	/* provide ddc symlink in sysfs */
329100163dfSAndrzej Pietrasiewicz 	connector->ddc = ddc;
330100163dfSAndrzej Pietrasiewicz 
331100163dfSAndrzej Pietrasiewicz 	return ret;
332100163dfSAndrzej Pietrasiewicz }
333100163dfSAndrzej Pietrasiewicz EXPORT_SYMBOL(drm_connector_init_with_ddc);
334100163dfSAndrzej Pietrasiewicz 
335100163dfSAndrzej Pietrasiewicz /**
3366b7e2d5cSGerd Hoffmann  * drm_connector_attach_edid_property - attach edid property.
3376b7e2d5cSGerd Hoffmann  * @connector: the connector
3386b7e2d5cSGerd Hoffmann  *
3396b7e2d5cSGerd Hoffmann  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
3406b7e2d5cSGerd Hoffmann  * edid property attached by default.  This function can be used to
3416b7e2d5cSGerd Hoffmann  * explicitly enable the edid property in these cases.
3426b7e2d5cSGerd Hoffmann  */
3436b7e2d5cSGerd Hoffmann void drm_connector_attach_edid_property(struct drm_connector *connector)
3446b7e2d5cSGerd Hoffmann {
3456b7e2d5cSGerd Hoffmann 	struct drm_mode_config *config = &connector->dev->mode_config;
3466b7e2d5cSGerd Hoffmann 
3476b7e2d5cSGerd Hoffmann 	drm_object_attach_property(&connector->base,
3486b7e2d5cSGerd Hoffmann 				   config->edid_property,
3496b7e2d5cSGerd Hoffmann 				   0);
3506b7e2d5cSGerd Hoffmann }
3516b7e2d5cSGerd Hoffmann EXPORT_SYMBOL(drm_connector_attach_edid_property);
3526b7e2d5cSGerd Hoffmann 
3536b7e2d5cSGerd Hoffmann /**
354cde4c44dSDaniel Vetter  * drm_connector_attach_encoder - attach a connector to an encoder
35552217195SDaniel Vetter  * @connector: connector to attach
35652217195SDaniel Vetter  * @encoder: encoder to attach @connector to
35752217195SDaniel Vetter  *
35852217195SDaniel Vetter  * This function links up a connector to an encoder. Note that the routing
35952217195SDaniel Vetter  * restrictions between encoders and crtcs are exposed to userspace through the
36052217195SDaniel Vetter  * possible_clones and possible_crtcs bitmasks.
36152217195SDaniel Vetter  *
36252217195SDaniel Vetter  * Returns:
36352217195SDaniel Vetter  * Zero on success, negative errno on failure.
36452217195SDaniel Vetter  */
365cde4c44dSDaniel Vetter int drm_connector_attach_encoder(struct drm_connector *connector,
36652217195SDaniel Vetter 				 struct drm_encoder *encoder)
36752217195SDaniel Vetter {
36852217195SDaniel Vetter 	/*
36952217195SDaniel Vetter 	 * In the past, drivers have attempted to model the static association
37052217195SDaniel Vetter 	 * of connector to encoder in simple connector/encoder devices using a
37152217195SDaniel Vetter 	 * direct assignment of connector->encoder = encoder. This connection
37252217195SDaniel Vetter 	 * is a logical one and the responsibility of the core, so drivers are
37352217195SDaniel Vetter 	 * expected not to mess with this.
37452217195SDaniel Vetter 	 *
37552217195SDaniel Vetter 	 * Note that the error return should've been enough here, but a large
37652217195SDaniel Vetter 	 * majority of drivers ignores the return value, so add in a big WARN
37752217195SDaniel Vetter 	 * to get people's attention.
37852217195SDaniel Vetter 	 */
37952217195SDaniel Vetter 	if (WARN_ON(connector->encoder))
38052217195SDaniel Vetter 		return -EINVAL;
38152217195SDaniel Vetter 
382*62afb4adSJosé Roberto de Souza 	connector->possible_encoders |= drm_encoder_mask(encoder);
383*62afb4adSJosé Roberto de Souza 
38452217195SDaniel Vetter 	return 0;
38552217195SDaniel Vetter }
386cde4c44dSDaniel Vetter EXPORT_SYMBOL(drm_connector_attach_encoder);
38752217195SDaniel Vetter 
38838cb8d96SVille Syrjälä /**
389*62afb4adSJosé Roberto de Souza  * drm_connector_has_possible_encoder - check if the connector and encoder are
390*62afb4adSJosé Roberto de Souza  * associated with each other
39138cb8d96SVille Syrjälä  * @connector: the connector
39238cb8d96SVille Syrjälä  * @encoder: the encoder
39338cb8d96SVille Syrjälä  *
39438cb8d96SVille Syrjälä  * Returns:
39538cb8d96SVille Syrjälä  * True if @encoder is one of the possible encoders for @connector.
39638cb8d96SVille Syrjälä  */
39738cb8d96SVille Syrjälä bool drm_connector_has_possible_encoder(struct drm_connector *connector,
39838cb8d96SVille Syrjälä 					struct drm_encoder *encoder)
39938cb8d96SVille Syrjälä {
400*62afb4adSJosé Roberto de Souza 	return connector->possible_encoders & drm_encoder_mask(encoder);
40138cb8d96SVille Syrjälä }
40238cb8d96SVille Syrjälä EXPORT_SYMBOL(drm_connector_has_possible_encoder);
40338cb8d96SVille Syrjälä 
40452217195SDaniel Vetter static void drm_mode_remove(struct drm_connector *connector,
40552217195SDaniel Vetter 			    struct drm_display_mode *mode)
40652217195SDaniel Vetter {
40752217195SDaniel Vetter 	list_del(&mode->head);
40852217195SDaniel Vetter 	drm_mode_destroy(connector->dev, mode);
40952217195SDaniel Vetter }
41052217195SDaniel Vetter 
41152217195SDaniel Vetter /**
41252217195SDaniel Vetter  * drm_connector_cleanup - cleans up an initialised connector
41352217195SDaniel Vetter  * @connector: connector to cleanup
41452217195SDaniel Vetter  *
41552217195SDaniel Vetter  * Cleans up the connector but doesn't free the object.
41652217195SDaniel Vetter  */
41752217195SDaniel Vetter void drm_connector_cleanup(struct drm_connector *connector)
41852217195SDaniel Vetter {
41952217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
42052217195SDaniel Vetter 	struct drm_display_mode *mode, *t;
42152217195SDaniel Vetter 
42252217195SDaniel Vetter 	/* The connector should have been removed from userspace long before
42352217195SDaniel Vetter 	 * it is finally destroyed.
42452217195SDaniel Vetter 	 */
42539b50c60SLyude Paul 	if (WARN_ON(connector->registration_state ==
42639b50c60SLyude Paul 		    DRM_CONNECTOR_REGISTERED))
42752217195SDaniel Vetter 		drm_connector_unregister(connector);
42852217195SDaniel Vetter 
42952217195SDaniel Vetter 	if (connector->tile_group) {
43052217195SDaniel Vetter 		drm_mode_put_tile_group(dev, connector->tile_group);
43152217195SDaniel Vetter 		connector->tile_group = NULL;
43252217195SDaniel Vetter 	}
43352217195SDaniel Vetter 
43452217195SDaniel Vetter 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
43552217195SDaniel Vetter 		drm_mode_remove(connector, mode);
43652217195SDaniel Vetter 
43752217195SDaniel Vetter 	list_for_each_entry_safe(mode, t, &connector->modes, head)
43852217195SDaniel Vetter 		drm_mode_remove(connector, mode);
43952217195SDaniel Vetter 
4409a47dba1SChristophe JAILLET 	ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
44152217195SDaniel Vetter 			  connector->connector_type_id);
44252217195SDaniel Vetter 
4439a47dba1SChristophe JAILLET 	ida_simple_remove(&dev->mode_config.connector_ida,
44452217195SDaniel Vetter 			  connector->index);
44552217195SDaniel Vetter 
44652217195SDaniel Vetter 	kfree(connector->display_info.bus_formats);
44752217195SDaniel Vetter 	drm_mode_object_unregister(dev, &connector->base);
44852217195SDaniel Vetter 	kfree(connector->name);
44952217195SDaniel Vetter 	connector->name = NULL;
450613051daSDaniel Vetter 	spin_lock_irq(&dev->mode_config.connector_list_lock);
45152217195SDaniel Vetter 	list_del(&connector->head);
45252217195SDaniel Vetter 	dev->mode_config.num_connector--;
453613051daSDaniel Vetter 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
45452217195SDaniel Vetter 
45552217195SDaniel Vetter 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
45652217195SDaniel Vetter 	if (connector->state && connector->funcs->atomic_destroy_state)
45752217195SDaniel Vetter 		connector->funcs->atomic_destroy_state(connector,
45852217195SDaniel Vetter 						       connector->state);
45952217195SDaniel Vetter 
460e73ab00eSDaniel Vetter 	mutex_destroy(&connector->mutex);
461e73ab00eSDaniel Vetter 
46252217195SDaniel Vetter 	memset(connector, 0, sizeof(*connector));
46352217195SDaniel Vetter }
46452217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_cleanup);
46552217195SDaniel Vetter 
46652217195SDaniel Vetter /**
46752217195SDaniel Vetter  * drm_connector_register - register a connector
46852217195SDaniel Vetter  * @connector: the connector to register
46952217195SDaniel Vetter  *
47052217195SDaniel Vetter  * Register userspace interfaces for a connector
47152217195SDaniel Vetter  *
47252217195SDaniel Vetter  * Returns:
47352217195SDaniel Vetter  * Zero on success, error code on failure.
47452217195SDaniel Vetter  */
47552217195SDaniel Vetter int drm_connector_register(struct drm_connector *connector)
47652217195SDaniel Vetter {
477e73ab00eSDaniel Vetter 	int ret = 0;
47852217195SDaniel Vetter 
479e6e7b48bSDaniel Vetter 	if (!connector->dev->registered)
480e6e7b48bSDaniel Vetter 		return 0;
481e6e7b48bSDaniel Vetter 
482e73ab00eSDaniel Vetter 	mutex_lock(&connector->mutex);
48339b50c60SLyude Paul 	if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
484e73ab00eSDaniel Vetter 		goto unlock;
48552217195SDaniel Vetter 
48652217195SDaniel Vetter 	ret = drm_sysfs_connector_add(connector);
48752217195SDaniel Vetter 	if (ret)
488e73ab00eSDaniel Vetter 		goto unlock;
48952217195SDaniel Vetter 
490b792e640SGreg Kroah-Hartman 	drm_debugfs_connector_add(connector);
49152217195SDaniel Vetter 
49252217195SDaniel Vetter 	if (connector->funcs->late_register) {
49352217195SDaniel Vetter 		ret = connector->funcs->late_register(connector);
49452217195SDaniel Vetter 		if (ret)
49552217195SDaniel Vetter 			goto err_debugfs;
49652217195SDaniel Vetter 	}
49752217195SDaniel Vetter 
49852217195SDaniel Vetter 	drm_mode_object_register(connector->dev, &connector->base);
49952217195SDaniel Vetter 
50039b50c60SLyude Paul 	connector->registration_state = DRM_CONNECTOR_REGISTERED;
501e73ab00eSDaniel Vetter 	goto unlock;
50252217195SDaniel Vetter 
50352217195SDaniel Vetter err_debugfs:
50452217195SDaniel Vetter 	drm_debugfs_connector_remove(connector);
50552217195SDaniel Vetter 	drm_sysfs_connector_remove(connector);
506e73ab00eSDaniel Vetter unlock:
507e73ab00eSDaniel Vetter 	mutex_unlock(&connector->mutex);
50852217195SDaniel Vetter 	return ret;
50952217195SDaniel Vetter }
51052217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_register);
51152217195SDaniel Vetter 
51252217195SDaniel Vetter /**
51352217195SDaniel Vetter  * drm_connector_unregister - unregister a connector
51452217195SDaniel Vetter  * @connector: the connector to unregister
51552217195SDaniel Vetter  *
51652217195SDaniel Vetter  * Unregister userspace interfaces for a connector
51752217195SDaniel Vetter  */
51852217195SDaniel Vetter void drm_connector_unregister(struct drm_connector *connector)
51952217195SDaniel Vetter {
520e73ab00eSDaniel Vetter 	mutex_lock(&connector->mutex);
52139b50c60SLyude Paul 	if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
522e73ab00eSDaniel Vetter 		mutex_unlock(&connector->mutex);
52352217195SDaniel Vetter 		return;
524e73ab00eSDaniel Vetter 	}
52552217195SDaniel Vetter 
52652217195SDaniel Vetter 	if (connector->funcs->early_unregister)
52752217195SDaniel Vetter 		connector->funcs->early_unregister(connector);
52852217195SDaniel Vetter 
52952217195SDaniel Vetter 	drm_sysfs_connector_remove(connector);
53052217195SDaniel Vetter 	drm_debugfs_connector_remove(connector);
53152217195SDaniel Vetter 
53239b50c60SLyude Paul 	connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
533e73ab00eSDaniel Vetter 	mutex_unlock(&connector->mutex);
53452217195SDaniel Vetter }
53552217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_unregister);
53652217195SDaniel Vetter 
53752217195SDaniel Vetter void drm_connector_unregister_all(struct drm_device *dev)
53852217195SDaniel Vetter {
53952217195SDaniel Vetter 	struct drm_connector *connector;
540613051daSDaniel Vetter 	struct drm_connector_list_iter conn_iter;
54152217195SDaniel Vetter 
542b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
543613051daSDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter)
54452217195SDaniel Vetter 		drm_connector_unregister(connector);
545b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
54652217195SDaniel Vetter }
54752217195SDaniel Vetter 
54852217195SDaniel Vetter int drm_connector_register_all(struct drm_device *dev)
54952217195SDaniel Vetter {
55052217195SDaniel Vetter 	struct drm_connector *connector;
551613051daSDaniel Vetter 	struct drm_connector_list_iter conn_iter;
552613051daSDaniel Vetter 	int ret = 0;
55352217195SDaniel Vetter 
554b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
555613051daSDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
55652217195SDaniel Vetter 		ret = drm_connector_register(connector);
55752217195SDaniel Vetter 		if (ret)
558613051daSDaniel Vetter 			break;
55952217195SDaniel Vetter 	}
560b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
56152217195SDaniel Vetter 
562613051daSDaniel Vetter 	if (ret)
56352217195SDaniel Vetter 		drm_connector_unregister_all(dev);
56452217195SDaniel Vetter 	return ret;
56552217195SDaniel Vetter }
56652217195SDaniel Vetter 
56752217195SDaniel Vetter /**
56852217195SDaniel Vetter  * drm_get_connector_status_name - return a string for connector status
56952217195SDaniel Vetter  * @status: connector status to compute name of
57052217195SDaniel Vetter  *
57152217195SDaniel Vetter  * In contrast to the other drm_get_*_name functions this one here returns a
57252217195SDaniel Vetter  * const pointer and hence is threadsafe.
57352217195SDaniel Vetter  */
57452217195SDaniel Vetter const char *drm_get_connector_status_name(enum drm_connector_status status)
57552217195SDaniel Vetter {
57652217195SDaniel Vetter 	if (status == connector_status_connected)
57752217195SDaniel Vetter 		return "connected";
57852217195SDaniel Vetter 	else if (status == connector_status_disconnected)
57952217195SDaniel Vetter 		return "disconnected";
58052217195SDaniel Vetter 	else
58152217195SDaniel Vetter 		return "unknown";
58252217195SDaniel Vetter }
58352217195SDaniel Vetter EXPORT_SYMBOL(drm_get_connector_status_name);
58452217195SDaniel Vetter 
5856140cf20SJani Nikula /**
5866140cf20SJani Nikula  * drm_get_connector_force_name - return a string for connector force
5876140cf20SJani Nikula  * @force: connector force to get name of
5886140cf20SJani Nikula  *
5896140cf20SJani Nikula  * Returns: const pointer to name.
5906140cf20SJani Nikula  */
5916140cf20SJani Nikula const char *drm_get_connector_force_name(enum drm_connector_force force)
5926140cf20SJani Nikula {
5936140cf20SJani Nikula 	switch (force) {
5946140cf20SJani Nikula 	case DRM_FORCE_UNSPECIFIED:
5956140cf20SJani Nikula 		return "unspecified";
5966140cf20SJani Nikula 	case DRM_FORCE_OFF:
5976140cf20SJani Nikula 		return "off";
5986140cf20SJani Nikula 	case DRM_FORCE_ON:
5996140cf20SJani Nikula 		return "on";
6006140cf20SJani Nikula 	case DRM_FORCE_ON_DIGITAL:
6016140cf20SJani Nikula 		return "digital";
6026140cf20SJani Nikula 	default:
6036140cf20SJani Nikula 		return "unknown";
6046140cf20SJani Nikula 	}
6056140cf20SJani Nikula }
6066140cf20SJani Nikula 
607613051daSDaniel Vetter #ifdef CONFIG_LOCKDEP
608613051daSDaniel Vetter static struct lockdep_map connector_list_iter_dep_map = {
609613051daSDaniel Vetter 	.name = "drm_connector_list_iter"
610613051daSDaniel Vetter };
611613051daSDaniel Vetter #endif
612613051daSDaniel Vetter 
613613051daSDaniel Vetter /**
614b982dab1SThierry Reding  * drm_connector_list_iter_begin - initialize a connector_list iterator
615613051daSDaniel Vetter  * @dev: DRM device
616613051daSDaniel Vetter  * @iter: connector_list iterator
617613051daSDaniel Vetter  *
618d574528aSDaniel Vetter  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
619b982dab1SThierry Reding  * must always be cleaned up again by calling drm_connector_list_iter_end().
620613051daSDaniel Vetter  * Iteration itself happens using drm_connector_list_iter_next() or
621613051daSDaniel Vetter  * drm_for_each_connector_iter().
622613051daSDaniel Vetter  */
623b982dab1SThierry Reding void drm_connector_list_iter_begin(struct drm_device *dev,
624613051daSDaniel Vetter 				   struct drm_connector_list_iter *iter)
625613051daSDaniel Vetter {
626613051daSDaniel Vetter 	iter->dev = dev;
627613051daSDaniel Vetter 	iter->conn = NULL;
628613051daSDaniel Vetter 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
629613051daSDaniel Vetter }
630b982dab1SThierry Reding EXPORT_SYMBOL(drm_connector_list_iter_begin);
631613051daSDaniel Vetter 
632a703c550SDaniel Vetter /*
633a703c550SDaniel Vetter  * Extra-safe connector put function that works in any context. Should only be
634a703c550SDaniel Vetter  * used from the connector_iter functions, where we never really expect to
635a703c550SDaniel Vetter  * actually release the connector when dropping our final reference.
636a703c550SDaniel Vetter  */
637a703c550SDaniel Vetter static void
638ea497bb9SDaniel Vetter __drm_connector_put_safe(struct drm_connector *conn)
639a703c550SDaniel Vetter {
640ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &conn->dev->mode_config;
641ea497bb9SDaniel Vetter 
642ea497bb9SDaniel Vetter 	lockdep_assert_held(&config->connector_list_lock);
643ea497bb9SDaniel Vetter 
644ea497bb9SDaniel Vetter 	if (!refcount_dec_and_test(&conn->base.refcount.refcount))
645ea497bb9SDaniel Vetter 		return;
646ea497bb9SDaniel Vetter 
647ea497bb9SDaniel Vetter 	llist_add(&conn->free_node, &config->connector_free_list);
648ea497bb9SDaniel Vetter 	schedule_work(&config->connector_free_work);
649a703c550SDaniel Vetter }
650a703c550SDaniel Vetter 
651613051daSDaniel Vetter /**
652613051daSDaniel Vetter  * drm_connector_list_iter_next - return next connector
6534f45c778SLyude Paul  * @iter: connector_list iterator
654613051daSDaniel Vetter  *
655613051daSDaniel Vetter  * Returns the next connector for @iter, or NULL when the list walk has
656613051daSDaniel Vetter  * completed.
657613051daSDaniel Vetter  */
658613051daSDaniel Vetter struct drm_connector *
659613051daSDaniel Vetter drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
660613051daSDaniel Vetter {
661613051daSDaniel Vetter 	struct drm_connector *old_conn = iter->conn;
662613051daSDaniel Vetter 	struct drm_mode_config *config = &iter->dev->mode_config;
663613051daSDaniel Vetter 	struct list_head *lhead;
664613051daSDaniel Vetter 	unsigned long flags;
665613051daSDaniel Vetter 
666613051daSDaniel Vetter 	spin_lock_irqsave(&config->connector_list_lock, flags);
667613051daSDaniel Vetter 	lhead = old_conn ? &old_conn->head : &config->connector_list;
668613051daSDaniel Vetter 
669613051daSDaniel Vetter 	do {
670613051daSDaniel Vetter 		if (lhead->next == &config->connector_list) {
671613051daSDaniel Vetter 			iter->conn = NULL;
672613051daSDaniel Vetter 			break;
673613051daSDaniel Vetter 		}
674613051daSDaniel Vetter 
675613051daSDaniel Vetter 		lhead = lhead->next;
676613051daSDaniel Vetter 		iter->conn = list_entry(lhead, struct drm_connector, head);
677613051daSDaniel Vetter 
678613051daSDaniel Vetter 		/* loop until it's not a zombie connector */
679613051daSDaniel Vetter 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
680613051daSDaniel Vetter 
681613051daSDaniel Vetter 	if (old_conn)
682ea497bb9SDaniel Vetter 		__drm_connector_put_safe(old_conn);
683ea497bb9SDaniel Vetter 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
684613051daSDaniel Vetter 
685613051daSDaniel Vetter 	return iter->conn;
686613051daSDaniel Vetter }
687613051daSDaniel Vetter EXPORT_SYMBOL(drm_connector_list_iter_next);
688613051daSDaniel Vetter 
689613051daSDaniel Vetter /**
690b982dab1SThierry Reding  * drm_connector_list_iter_end - tear down a connector_list iterator
691613051daSDaniel Vetter  * @iter: connector_list iterator
692613051daSDaniel Vetter  *
693613051daSDaniel Vetter  * Tears down @iter and releases any resources (like &drm_connector references)
694613051daSDaniel Vetter  * acquired while walking the list. This must always be called, both when the
695613051daSDaniel Vetter  * iteration completes fully or when it was aborted without walking the entire
696613051daSDaniel Vetter  * list.
697613051daSDaniel Vetter  */
698b982dab1SThierry Reding void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
699613051daSDaniel Vetter {
700ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &iter->dev->mode_config;
701ea497bb9SDaniel Vetter 	unsigned long flags;
702ea497bb9SDaniel Vetter 
703613051daSDaniel Vetter 	iter->dev = NULL;
704ea497bb9SDaniel Vetter 	if (iter->conn) {
705ea497bb9SDaniel Vetter 		spin_lock_irqsave(&config->connector_list_lock, flags);
706ea497bb9SDaniel Vetter 		__drm_connector_put_safe(iter->conn);
707ea497bb9SDaniel Vetter 		spin_unlock_irqrestore(&config->connector_list_lock, flags);
708ea497bb9SDaniel Vetter 	}
709613051daSDaniel Vetter 	lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
710613051daSDaniel Vetter }
711b982dab1SThierry Reding EXPORT_SYMBOL(drm_connector_list_iter_end);
712613051daSDaniel Vetter 
71352217195SDaniel Vetter static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
71452217195SDaniel Vetter 	{ SubPixelUnknown, "Unknown" },
71552217195SDaniel Vetter 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
71652217195SDaniel Vetter 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
71752217195SDaniel Vetter 	{ SubPixelVerticalRGB, "Vertical RGB" },
71852217195SDaniel Vetter 	{ SubPixelVerticalBGR, "Vertical BGR" },
71952217195SDaniel Vetter 	{ SubPixelNone, "None" },
72052217195SDaniel Vetter };
72152217195SDaniel Vetter 
72252217195SDaniel Vetter /**
72352217195SDaniel Vetter  * drm_get_subpixel_order_name - return a string for a given subpixel enum
72452217195SDaniel Vetter  * @order: enum of subpixel_order
72552217195SDaniel Vetter  *
72652217195SDaniel Vetter  * Note you could abuse this and return something out of bounds, but that
72752217195SDaniel Vetter  * would be a caller error.  No unscrubbed user data should make it here.
72852217195SDaniel Vetter  */
72952217195SDaniel Vetter const char *drm_get_subpixel_order_name(enum subpixel_order order)
73052217195SDaniel Vetter {
73152217195SDaniel Vetter 	return drm_subpixel_enum_list[order].name;
73252217195SDaniel Vetter }
73352217195SDaniel Vetter EXPORT_SYMBOL(drm_get_subpixel_order_name);
73452217195SDaniel Vetter 
73552217195SDaniel Vetter static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
73652217195SDaniel Vetter 	{ DRM_MODE_DPMS_ON, "On" },
73752217195SDaniel Vetter 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
73852217195SDaniel Vetter 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
73952217195SDaniel Vetter 	{ DRM_MODE_DPMS_OFF, "Off" }
74052217195SDaniel Vetter };
74152217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
74252217195SDaniel Vetter 
74340ee6fbeSManasi Navare static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
74440ee6fbeSManasi Navare 	{ DRM_MODE_LINK_STATUS_GOOD, "Good" },
74540ee6fbeSManasi Navare 	{ DRM_MODE_LINK_STATUS_BAD, "Bad" },
74640ee6fbeSManasi Navare };
74740ee6fbeSManasi Navare 
748b3c6c8bfSDaniel Vetter /**
749b3c6c8bfSDaniel Vetter  * drm_display_info_set_bus_formats - set the supported bus formats
750b3c6c8bfSDaniel Vetter  * @info: display info to store bus formats in
751b3c6c8bfSDaniel Vetter  * @formats: array containing the supported bus formats
752b3c6c8bfSDaniel Vetter  * @num_formats: the number of entries in the fmts array
753b3c6c8bfSDaniel Vetter  *
754b3c6c8bfSDaniel Vetter  * Store the supported bus formats in display info structure.
755b3c6c8bfSDaniel Vetter  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
756b3c6c8bfSDaniel Vetter  * a full list of available formats.
757b3c6c8bfSDaniel Vetter  */
758b3c6c8bfSDaniel Vetter int drm_display_info_set_bus_formats(struct drm_display_info *info,
759b3c6c8bfSDaniel Vetter 				     const u32 *formats,
760b3c6c8bfSDaniel Vetter 				     unsigned int num_formats)
761b3c6c8bfSDaniel Vetter {
762b3c6c8bfSDaniel Vetter 	u32 *fmts = NULL;
763b3c6c8bfSDaniel Vetter 
764b3c6c8bfSDaniel Vetter 	if (!formats && num_formats)
765b3c6c8bfSDaniel Vetter 		return -EINVAL;
766b3c6c8bfSDaniel Vetter 
767b3c6c8bfSDaniel Vetter 	if (formats && num_formats) {
768b3c6c8bfSDaniel Vetter 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
769b3c6c8bfSDaniel Vetter 			       GFP_KERNEL);
770b3c6c8bfSDaniel Vetter 		if (!fmts)
771b3c6c8bfSDaniel Vetter 			return -ENOMEM;
772b3c6c8bfSDaniel Vetter 	}
773b3c6c8bfSDaniel Vetter 
774b3c6c8bfSDaniel Vetter 	kfree(info->bus_formats);
775b3c6c8bfSDaniel Vetter 	info->bus_formats = fmts;
776b3c6c8bfSDaniel Vetter 	info->num_bus_formats = num_formats;
777b3c6c8bfSDaniel Vetter 
778b3c6c8bfSDaniel Vetter 	return 0;
779b3c6c8bfSDaniel Vetter }
780b3c6c8bfSDaniel Vetter EXPORT_SYMBOL(drm_display_info_set_bus_formats);
781b3c6c8bfSDaniel Vetter 
78252217195SDaniel Vetter /* Optional connector properties. */
78352217195SDaniel Vetter static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
78452217195SDaniel Vetter 	{ DRM_MODE_SCALE_NONE, "None" },
78552217195SDaniel Vetter 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
78652217195SDaniel Vetter 	{ DRM_MODE_SCALE_CENTER, "Center" },
78752217195SDaniel Vetter 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
78852217195SDaniel Vetter };
78952217195SDaniel Vetter 
79052217195SDaniel Vetter static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
79152217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
79252217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
79352217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
79452217195SDaniel Vetter };
79552217195SDaniel Vetter 
79650525c33SStanislav Lisovskiy static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
79750525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
79850525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
79950525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
80050525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
80150525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_GAME, "Game" },
80250525c33SStanislav Lisovskiy };
80350525c33SStanislav Lisovskiy 
8048d70f395SHans de Goede static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
8058d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_NORMAL,	"Normal"	},
8068d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP,	"Upside Down"	},
8078d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_LEFT_UP,	"Left Side Up"	},
8088d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,	"Right Side Up"	},
8098d70f395SHans de Goede };
8108d70f395SHans de Goede 
81152217195SDaniel Vetter static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
81252217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
81352217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
81452217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
81552217195SDaniel Vetter };
81652217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
81752217195SDaniel Vetter 
81852217195SDaniel Vetter static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
81952217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
82052217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
82152217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
82252217195SDaniel Vetter };
82352217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
82452217195SDaniel Vetter 		 drm_dvi_i_subconnector_enum_list)
82552217195SDaniel Vetter 
82652217195SDaniel Vetter static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
82752217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
82852217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
82952217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
83052217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
83152217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
83252217195SDaniel Vetter };
83352217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
83452217195SDaniel Vetter 
83552217195SDaniel Vetter static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
83652217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
83752217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
83852217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
83952217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
84052217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
84152217195SDaniel Vetter };
84252217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
84352217195SDaniel Vetter 		 drm_tv_subconnector_enum_list)
84452217195SDaniel Vetter 
845d2c6a405SUma Shankar static const struct drm_prop_enum_list hdmi_colorspaces[] = {
846d2c6a405SUma Shankar 	/* For Default case, driver will set the colorspace */
847d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
848d2c6a405SUma Shankar 	/* Standard Definition Colorimetry based on CEA 861 */
849d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_SMPTE_170M_YCC, "SMPTE_170M_YCC" },
850d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
851d2c6a405SUma Shankar 	/* Standard Definition Colorimetry based on IEC 61966-2-4 */
852d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
853d2c6a405SUma Shankar 	/* High Definition Colorimetry based on IEC 61966-2-4 */
854d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
855d2c6a405SUma Shankar 	/* Colorimetry based on IEC 61966-2-1/Amendment 1 */
856d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
857d2c6a405SUma Shankar 	/* Colorimetry based on IEC 61966-2-5 [33] */
858d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
859d2c6a405SUma Shankar 	/* Colorimetry based on IEC 61966-2-5 */
860d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
861d2c6a405SUma Shankar 	/* Colorimetry based on ITU-R BT.2020 */
862d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
863d2c6a405SUma Shankar 	/* Colorimetry based on ITU-R BT.2020 */
864d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
865d2c6a405SUma Shankar 	/* Colorimetry based on ITU-R BT.2020 */
866d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
867d2c6a405SUma Shankar 	/* Added as part of Additional Colorimetry Extension in 861.G */
868d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
869d2c6a405SUma Shankar 	{ DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER, "DCI-P3_RGB_Theater" },
870d2c6a405SUma Shankar };
871d2c6a405SUma Shankar 
8724ada6f22SDaniel Vetter /**
8734ada6f22SDaniel Vetter  * DOC: standard connector properties
8744ada6f22SDaniel Vetter  *
8754ada6f22SDaniel Vetter  * DRM connectors have a few standardized properties:
8764ada6f22SDaniel Vetter  *
8774ada6f22SDaniel Vetter  * EDID:
8784ada6f22SDaniel Vetter  * 	Blob property which contains the current EDID read from the sink. This
8794ada6f22SDaniel Vetter  * 	is useful to parse sink identification information like vendor, model
8804ada6f22SDaniel Vetter  * 	and serial. Drivers should update this property by calling
881c555f023SDaniel Vetter  * 	drm_connector_update_edid_property(), usually after having parsed
8824ada6f22SDaniel Vetter  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
8834ada6f22SDaniel Vetter  * 	property.
8844ada6f22SDaniel Vetter  * DPMS:
8854ada6f22SDaniel Vetter  * 	Legacy property for setting the power state of the connector. For atomic
8864ada6f22SDaniel Vetter  * 	drivers this is only provided for backwards compatibility with existing
8874ada6f22SDaniel Vetter  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
8884ada6f22SDaniel Vetter  * 	connector is linked to. Drivers should never set this property directly,
889d574528aSDaniel Vetter  * 	it is handled by the DRM core by calling the &drm_connector_funcs.dpms
890144a7999SDaniel Vetter  * 	callback. For atomic drivers the remapping to the "ACTIVE" property is
891144a7999SDaniel Vetter  * 	implemented in the DRM core.  This is the only standard connector
892144a7999SDaniel Vetter  * 	property that userspace can change.
893d0d1aee5SDaniel Vetter  *
894d0d1aee5SDaniel Vetter  * 	Note that this property cannot be set through the MODE_ATOMIC ioctl,
895d0d1aee5SDaniel Vetter  * 	userspace must use "ACTIVE" on the CRTC instead.
896d0d1aee5SDaniel Vetter  *
897d0d1aee5SDaniel Vetter  * 	WARNING:
898d0d1aee5SDaniel Vetter  *
899d0d1aee5SDaniel Vetter  * 	For userspace also running on legacy drivers the "DPMS" semantics are a
900d0d1aee5SDaniel Vetter  * 	lot more complicated. First, userspace cannot rely on the "DPMS" value
901d0d1aee5SDaniel Vetter  * 	returned by the GETCONNECTOR actually reflecting reality, because many
902d0d1aee5SDaniel Vetter  * 	drivers fail to update it. For atomic drivers this is taken care of in
903d0d1aee5SDaniel Vetter  * 	drm_atomic_helper_update_legacy_modeset_state().
904d0d1aee5SDaniel Vetter  *
905d0d1aee5SDaniel Vetter  * 	The second issue is that the DPMS state is only well-defined when the
906d0d1aee5SDaniel Vetter  * 	connector is connected to a CRTC. In atomic the DRM core enforces that
907d0d1aee5SDaniel Vetter  * 	"ACTIVE" is off in such a case, no such checks exists for "DPMS".
908d0d1aee5SDaniel Vetter  *
909d0d1aee5SDaniel Vetter  * 	Finally, when enabling an output using the legacy SETCONFIG ioctl then
910d0d1aee5SDaniel Vetter  * 	"DPMS" is forced to ON. But see above, that might not be reflected in
911d0d1aee5SDaniel Vetter  * 	the software value on legacy drivers.
912d0d1aee5SDaniel Vetter  *
913d0d1aee5SDaniel Vetter  * 	Summarizing: Only set "DPMS" when the connector is known to be enabled,
914d0d1aee5SDaniel Vetter  * 	assume that a successful SETCONFIG call also sets "DPMS" to on, and
915d0d1aee5SDaniel Vetter  * 	never read back the value of "DPMS" because it can be incorrect.
9164ada6f22SDaniel Vetter  * PATH:
9174ada6f22SDaniel Vetter  * 	Connector path property to identify how this sink is physically
9184ada6f22SDaniel Vetter  * 	connected. Used by DP MST. This should be set by calling
91997e14fbeSDaniel Vetter  * 	drm_connector_set_path_property(), in the case of DP MST with the
9204ada6f22SDaniel Vetter  * 	path property the MST manager created. Userspace cannot change this
9214ada6f22SDaniel Vetter  * 	property.
9224ada6f22SDaniel Vetter  * TILE:
9234ada6f22SDaniel Vetter  * 	Connector tile group property to indicate how a set of DRM connector
9244ada6f22SDaniel Vetter  * 	compose together into one logical screen. This is used by both high-res
9254ada6f22SDaniel Vetter  * 	external screens (often only using a single cable, but exposing multiple
9264ada6f22SDaniel Vetter  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
9274ada6f22SDaniel Vetter  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
9284ada6f22SDaniel Vetter  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
9294ada6f22SDaniel Vetter  * 	tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
93097e14fbeSDaniel Vetter  * 	should update this value using drm_connector_set_tile_property().
9314ada6f22SDaniel Vetter  * 	Userspace cannot change this property.
93240ee6fbeSManasi Navare  * link-status:
933716719a3SSean Paul  *      Connector link-status property to indicate the status of link. The
934716719a3SSean Paul  *      default value of link-status is "GOOD". If something fails during or
935716719a3SSean Paul  *      after modeset, the kernel driver may set this to "BAD" and issue a
936716719a3SSean Paul  *      hotplug uevent. Drivers should update this value using
93797e14fbeSDaniel Vetter  *      drm_connector_set_link_status_property().
93866660d4cSDave Airlie  * non_desktop:
93966660d4cSDave Airlie  * 	Indicates the output should be ignored for purposes of displaying a
94066660d4cSDave Airlie  * 	standard desktop environment or console. This is most likely because
94166660d4cSDave Airlie  * 	the output device is not rectilinear.
94224557865SSean Paul  * Content Protection:
94324557865SSean Paul  *	This property is used by userspace to request the kernel protect future
94424557865SSean Paul  *	content communicated over the link. When requested, kernel will apply
94524557865SSean Paul  *	the appropriate means of protection (most often HDCP), and use the
94624557865SSean Paul  *	property to tell userspace the protection is active.
94724557865SSean Paul  *
94824557865SSean Paul  *	Drivers can set this up by calling
94924557865SSean Paul  *	drm_connector_attach_content_protection_property() on initialization.
95024557865SSean Paul  *
95124557865SSean Paul  *	The value of this property can be one of the following:
95224557865SSean Paul  *
953bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
95424557865SSean Paul  *		The link is not protected, content is transmitted in the clear.
955bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
95624557865SSean Paul  *		Userspace has requested content protection, but the link is not
95724557865SSean Paul  *		currently protected. When in this state, kernel should enable
95824557865SSean Paul  *		Content Protection as soon as possible.
959bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
96024557865SSean Paul  *		Userspace has requested content protection, and the link is
96124557865SSean Paul  *		protected. Only the driver can set the property to this value.
96224557865SSean Paul  *		If userspace attempts to set to ENABLED, kernel will return
96324557865SSean Paul  *		-EINVAL.
96424557865SSean Paul  *
96524557865SSean Paul  *	A few guidelines:
96624557865SSean Paul  *
96724557865SSean Paul  *	- DESIRED state should be preserved until userspace de-asserts it by
96824557865SSean Paul  *	  setting the property to UNDESIRED. This means ENABLED should only
96924557865SSean Paul  *	  transition to UNDESIRED when the user explicitly requests it.
97024557865SSean Paul  *	- If the state is DESIRED, kernel should attempt to re-authenticate the
97124557865SSean Paul  *	  link whenever possible. This includes across disable/enable, dpms,
97224557865SSean Paul  *	  hotplug, downstream device changes, link status failures, etc..
973bb5a45d4SRamalingam C  *	- Kernel sends uevent with the connector id and property id through
974bb5a45d4SRamalingam C  *	  @drm_hdcp_update_content_protection, upon below kernel triggered
975bb5a45d4SRamalingam C  *	  scenarios:
97612db36bcSSean Paul  *
97712db36bcSSean Paul  *		- DESIRED -> ENABLED (authentication success)
97812db36bcSSean Paul  *		- ENABLED -> DESIRED (termination of authentication)
979bb5a45d4SRamalingam C  *	- Please note no uevents for userspace triggered property state changes,
980bb5a45d4SRamalingam C  *	  which can't fail such as
98112db36bcSSean Paul  *
98212db36bcSSean Paul  *		- DESIRED/ENABLED -> UNDESIRED
98312db36bcSSean Paul  *		- UNDESIRED -> DESIRED
984bb5a45d4SRamalingam C  *	- Userspace is responsible for polling the property or listen to uevents
985bb5a45d4SRamalingam C  *	  to determine when the value transitions from ENABLED to DESIRED.
986bb5a45d4SRamalingam C  *	  This signifies the link is no longer protected and userspace should
987bb5a45d4SRamalingam C  *	  take appropriate action (whatever that might be).
9884ada6f22SDaniel Vetter  *
9897672dbbaSRamalingam C  * HDCP Content Type:
9907672dbbaSRamalingam C  *	This Enum property is used by the userspace to declare the content type
9917672dbbaSRamalingam C  *	of the display stream, to kernel. Here display stream stands for any
9927672dbbaSRamalingam C  *	display content that userspace intended to display through HDCP
9937672dbbaSRamalingam C  *	encryption.
9947672dbbaSRamalingam C  *
9957672dbbaSRamalingam C  *	Content Type of a stream is decided by the owner of the stream, as
9967672dbbaSRamalingam C  *	"HDCP Type0" or "HDCP Type1".
9977672dbbaSRamalingam C  *
9987672dbbaSRamalingam C  *	The value of the property can be one of the below:
9997672dbbaSRamalingam C  *	  - "HDCP Type0": DRM_MODE_HDCP_CONTENT_TYPE0 = 0
10007672dbbaSRamalingam C  *	  - "HDCP Type1": DRM_MODE_HDCP_CONTENT_TYPE1 = 1
10017672dbbaSRamalingam C  *
10027672dbbaSRamalingam C  *	When kernel starts the HDCP authentication (see "Content Protection"
10037672dbbaSRamalingam C  *	for details), it uses the content type in "HDCP Content Type"
10047672dbbaSRamalingam C  *	for performing the HDCP authentication with the display sink.
10057672dbbaSRamalingam C  *
10067672dbbaSRamalingam C  *	Please note in HDCP spec versions, a link can be authenticated with
10077672dbbaSRamalingam C  *	HDCP 2.2 for Content Type 0/Content Type 1. Where as a link can be
10087672dbbaSRamalingam C  *	authenticated with HDCP1.4 only for Content Type 0(though it is implicit
10097672dbbaSRamalingam C  *	in nature. As there is no reference for Content Type in HDCP1.4).
10107672dbbaSRamalingam C  *
10117672dbbaSRamalingam C  *	HDCP2.2 authentication protocol itself takes the "Content Type" as a
10127672dbbaSRamalingam C  *	parameter, which is a input for the DP HDCP2.2 encryption algo.
10137672dbbaSRamalingam C  *
10147672dbbaSRamalingam C  *	In case of Type 0 content protection request, kernel driver can choose
10157672dbbaSRamalingam C  *	either of HDCP spec versions 1.4 and 2.2. When HDCP2.2 is used for
10167672dbbaSRamalingam C  *	"HDCP Type 0", a HDCP 2.2 capable repeater in the downstream can send
10177672dbbaSRamalingam C  *	that content to a HDCP 1.4 authenticated HDCP sink (Type0 link).
10187672dbbaSRamalingam C  *	But if the content is classified as "HDCP Type 1", above mentioned
10197672dbbaSRamalingam C  *	HDCP 2.2 repeater wont send the content to the HDCP sink as it can't
10207672dbbaSRamalingam C  *	authenticate the HDCP1.4 capable sink for "HDCP Type 1".
10217672dbbaSRamalingam C  *
10227672dbbaSRamalingam C  *	Please note userspace can be ignorant of the HDCP versions used by the
10237672dbbaSRamalingam C  *	kernel driver to achieve the "HDCP Content Type".
10247672dbbaSRamalingam C  *
10257672dbbaSRamalingam C  *	At current scenario, classifying a content as Type 1 ensures that the
10267672dbbaSRamalingam C  *	content will be displayed only through the HDCP2.2 encrypted link.
10277672dbbaSRamalingam C  *
10287672dbbaSRamalingam C  *	Note that the HDCP Content Type property is introduced at HDCP 2.2, and
10297672dbbaSRamalingam C  *	defaults to type 0. It is only exposed by drivers supporting HDCP 2.2
10307672dbbaSRamalingam C  *	(hence supporting Type 0 and Type 1). Based on how next versions of
10317672dbbaSRamalingam C  *	HDCP specs are defined content Type could be used for higher versions
10327672dbbaSRamalingam C  *	too.
10337672dbbaSRamalingam C  *
10347672dbbaSRamalingam C  *	If content type is changed when "Content Protection" is not UNDESIRED,
10357672dbbaSRamalingam C  *	then kernel will disable the HDCP and re-enable with new type in the
10367672dbbaSRamalingam C  *	same atomic commit. And when "Content Protection" is ENABLED, it means
10377672dbbaSRamalingam C  *	that link is HDCP authenticated and encrypted, for the transmission of
10387672dbbaSRamalingam C  *	the Type of stream mentioned at "HDCP Content Type".
10397672dbbaSRamalingam C  *
1040a09db883SUma Shankar  * HDR_OUTPUT_METADATA:
1041a09db883SUma Shankar  *	Connector property to enable userspace to send HDR Metadata to
1042a09db883SUma Shankar  *	driver. This metadata is based on the composition and blending
1043a09db883SUma Shankar  *	policies decided by user, taking into account the hardware and
1044a09db883SUma Shankar  *	sink capabilities. The driver gets this metadata and creates a
1045a09db883SUma Shankar  *	Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
1046a09db883SUma Shankar  *	SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
1047a09db883SUma Shankar  *	sent to sink. This notifies the sink of the upcoming frame's Color
1048a09db883SUma Shankar  *	Encoding and Luminance parameters.
1049a09db883SUma Shankar  *
1050a09db883SUma Shankar  *	Userspace first need to detect the HDR capabilities of sink by
1051a09db883SUma Shankar  *	reading and parsing the EDID. Details of HDR metadata for HDMI
1052a09db883SUma Shankar  *	are added in CTA 861.G spec. For DP , its defined in VESA DP
1053a09db883SUma Shankar  *	Standard v1.4. It needs to then get the metadata information
1054a09db883SUma Shankar  *	of the video/game/app content which are encoded in HDR (basically
1055a09db883SUma Shankar  *	using HDR transfer functions). With this information it needs to
1056a09db883SUma Shankar  *	decide on a blending policy and compose the relevant
1057a09db883SUma Shankar  *	layers/overlays into a common format. Once this blending is done,
1058a09db883SUma Shankar  *	userspace will be aware of the metadata of the composed frame to
1059a09db883SUma Shankar  *	be send to sink. It then uses this property to communicate this
1060a09db883SUma Shankar  *	metadata to driver which then make a Infoframe packet and sends
1061a09db883SUma Shankar  *	to sink based on the type of encoder connected.
1062a09db883SUma Shankar  *
1063a09db883SUma Shankar  *	Userspace will be responsible to do Tone mapping operation in case:
1064a09db883SUma Shankar  *		- Some layers are HDR and others are SDR
1065a09db883SUma Shankar  *		- HDR layers luminance is not same as sink
10669f9b2559SSean Paul  *
1067a09db883SUma Shankar  *	It will even need to do colorspace conversion and get all layers
1068a09db883SUma Shankar  *	to one common colorspace for blending. It can use either GL, Media
1069a09db883SUma Shankar  *	or display engine to get this done based on the capabilties of the
1070a09db883SUma Shankar  *	associated hardware.
1071a09db883SUma Shankar  *
1072a09db883SUma Shankar  *	Driver expects metadata to be put in &struct hdr_output_metadata
1073a09db883SUma Shankar  *	structure from userspace. This is received as blob and stored in
1074a09db883SUma Shankar  *	&drm_connector_state.hdr_output_metadata. It parses EDID and saves the
1075a09db883SUma Shankar  *	sink metadata in &struct hdr_sink_metadata, as
1076a09db883SUma Shankar  *	&drm_connector.hdr_sink_metadata.  Driver uses
1077a09db883SUma Shankar  *	drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
1078a09db883SUma Shankar  *	hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
1079a09db883SUma Shankar  *	HDMI encoder.
1080a09db883SUma Shankar  *
108147e22ff1SRadhakrishna Sripada  * max bpc:
108247e22ff1SRadhakrishna Sripada  *	This range property is used by userspace to limit the bit depth. When
108347e22ff1SRadhakrishna Sripada  *	used the driver would limit the bpc in accordance with the valid range
108447e22ff1SRadhakrishna Sripada  *	supported by the hardware and sink. Drivers to use the function
108547e22ff1SRadhakrishna Sripada  *	drm_connector_attach_max_bpc_property() to create and attach the
108647e22ff1SRadhakrishna Sripada  *	property to the connector during initialization.
108747e22ff1SRadhakrishna Sripada  *
10884ada6f22SDaniel Vetter  * Connectors also have one standardized atomic property:
10894ada6f22SDaniel Vetter  *
10904ada6f22SDaniel Vetter  * CRTC_ID:
10914ada6f22SDaniel Vetter  * 	Mode object ID of the &drm_crtc this connector should be connected to.
10928d70f395SHans de Goede  *
10938d70f395SHans de Goede  * Connectors for LCD panels may also have one standardized property:
10948d70f395SHans de Goede  *
10958d70f395SHans de Goede  * panel orientation:
10968d70f395SHans de Goede  *	On some devices the LCD panel is mounted in the casing in such a way
10978d70f395SHans de Goede  *	that the up/top side of the panel does not match with the top side of
10988d70f395SHans de Goede  *	the device. Userspace can use this property to check for this.
10998d70f395SHans de Goede  *	Note that input coordinates from touchscreens (input devices with
11008d70f395SHans de Goede  *	INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
11018d70f395SHans de Goede  *	coordinates, so if userspace rotates the picture to adjust for
11028d70f395SHans de Goede  *	the orientation it must also apply the same transformation to the
1103bbeba09fSDaniel Vetter  *	touchscreen input coordinates. This property is initialized by calling
1104bbeba09fSDaniel Vetter  *	drm_connector_init_panel_orientation_property().
1105bbeba09fSDaniel Vetter  *
1106bbeba09fSDaniel Vetter  * scaling mode:
1107bbeba09fSDaniel Vetter  *	This property defines how a non-native mode is upscaled to the native
1108bbeba09fSDaniel Vetter  *	mode of an LCD panel:
1109bbeba09fSDaniel Vetter  *
1110bbeba09fSDaniel Vetter  *	None:
1111bbeba09fSDaniel Vetter  *		No upscaling happens, scaling is left to the panel. Not all
1112bbeba09fSDaniel Vetter  *		drivers expose this mode.
1113bbeba09fSDaniel Vetter  *	Full:
1114bbeba09fSDaniel Vetter  *		The output is upscaled to the full resolution of the panel,
1115bbeba09fSDaniel Vetter  *		ignoring the aspect ratio.
1116bbeba09fSDaniel Vetter  *	Center:
1117bbeba09fSDaniel Vetter  *		No upscaling happens, the output is centered within the native
1118bbeba09fSDaniel Vetter  *		resolution the panel.
1119bbeba09fSDaniel Vetter  *	Full aspect:
1120bbeba09fSDaniel Vetter  *		The output is upscaled to maximize either the width or height
1121bbeba09fSDaniel Vetter  *		while retaining the aspect ratio.
1122bbeba09fSDaniel Vetter  *
1123bbeba09fSDaniel Vetter  *	This property should be set up by calling
1124bbeba09fSDaniel Vetter  *	drm_connector_attach_scaling_mode_property(). Note that drivers
1125bbeba09fSDaniel Vetter  *	can also expose this property to external outputs, in which case they
1126bbeba09fSDaniel Vetter  *	must support "None", which should be the default (since external screens
1127bbeba09fSDaniel Vetter  *	have a built-in scaler).
11284ada6f22SDaniel Vetter  */
11294ada6f22SDaniel Vetter 
113052217195SDaniel Vetter int drm_connector_create_standard_properties(struct drm_device *dev)
113152217195SDaniel Vetter {
113252217195SDaniel Vetter 	struct drm_property *prop;
113352217195SDaniel Vetter 
113452217195SDaniel Vetter 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
113552217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
113652217195SDaniel Vetter 				   "EDID", 0);
113752217195SDaniel Vetter 	if (!prop)
113852217195SDaniel Vetter 		return -ENOMEM;
113952217195SDaniel Vetter 	dev->mode_config.edid_property = prop;
114052217195SDaniel Vetter 
114152217195SDaniel Vetter 	prop = drm_property_create_enum(dev, 0,
114252217195SDaniel Vetter 				   "DPMS", drm_dpms_enum_list,
114352217195SDaniel Vetter 				   ARRAY_SIZE(drm_dpms_enum_list));
114452217195SDaniel Vetter 	if (!prop)
114552217195SDaniel Vetter 		return -ENOMEM;
114652217195SDaniel Vetter 	dev->mode_config.dpms_property = prop;
114752217195SDaniel Vetter 
114852217195SDaniel Vetter 	prop = drm_property_create(dev,
114952217195SDaniel Vetter 				   DRM_MODE_PROP_BLOB |
115052217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
115152217195SDaniel Vetter 				   "PATH", 0);
115252217195SDaniel Vetter 	if (!prop)
115352217195SDaniel Vetter 		return -ENOMEM;
115452217195SDaniel Vetter 	dev->mode_config.path_property = prop;
115552217195SDaniel Vetter 
115652217195SDaniel Vetter 	prop = drm_property_create(dev,
115752217195SDaniel Vetter 				   DRM_MODE_PROP_BLOB |
115852217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
115952217195SDaniel Vetter 				   "TILE", 0);
116052217195SDaniel Vetter 	if (!prop)
116152217195SDaniel Vetter 		return -ENOMEM;
116252217195SDaniel Vetter 	dev->mode_config.tile_property = prop;
116352217195SDaniel Vetter 
116440ee6fbeSManasi Navare 	prop = drm_property_create_enum(dev, 0, "link-status",
116540ee6fbeSManasi Navare 					drm_link_status_enum_list,
116640ee6fbeSManasi Navare 					ARRAY_SIZE(drm_link_status_enum_list));
116740ee6fbeSManasi Navare 	if (!prop)
116840ee6fbeSManasi Navare 		return -ENOMEM;
116940ee6fbeSManasi Navare 	dev->mode_config.link_status_property = prop;
117040ee6fbeSManasi Navare 
117166660d4cSDave Airlie 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
117266660d4cSDave Airlie 	if (!prop)
117366660d4cSDave Airlie 		return -ENOMEM;
117466660d4cSDave Airlie 	dev->mode_config.non_desktop_property = prop;
117566660d4cSDave Airlie 
1176fbb5d035SUma Shankar 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1177fbb5d035SUma Shankar 				   "HDR_OUTPUT_METADATA", 0);
1178fbb5d035SUma Shankar 	if (!prop)
1179fbb5d035SUma Shankar 		return -ENOMEM;
1180fbb5d035SUma Shankar 	dev->mode_config.hdr_output_metadata_property = prop;
1181fbb5d035SUma Shankar 
118252217195SDaniel Vetter 	return 0;
118352217195SDaniel Vetter }
118452217195SDaniel Vetter 
118552217195SDaniel Vetter /**
118652217195SDaniel Vetter  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
118752217195SDaniel Vetter  * @dev: DRM device
118852217195SDaniel Vetter  *
118952217195SDaniel Vetter  * Called by a driver the first time a DVI-I connector is made.
119052217195SDaniel Vetter  */
119152217195SDaniel Vetter int drm_mode_create_dvi_i_properties(struct drm_device *dev)
119252217195SDaniel Vetter {
119352217195SDaniel Vetter 	struct drm_property *dvi_i_selector;
119452217195SDaniel Vetter 	struct drm_property *dvi_i_subconnector;
119552217195SDaniel Vetter 
119652217195SDaniel Vetter 	if (dev->mode_config.dvi_i_select_subconnector_property)
119752217195SDaniel Vetter 		return 0;
119852217195SDaniel Vetter 
119952217195SDaniel Vetter 	dvi_i_selector =
120052217195SDaniel Vetter 		drm_property_create_enum(dev, 0,
120152217195SDaniel Vetter 				    "select subconnector",
120252217195SDaniel Vetter 				    drm_dvi_i_select_enum_list,
120352217195SDaniel Vetter 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
120452217195SDaniel Vetter 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
120552217195SDaniel Vetter 
120652217195SDaniel Vetter 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
120752217195SDaniel Vetter 				    "subconnector",
120852217195SDaniel Vetter 				    drm_dvi_i_subconnector_enum_list,
120952217195SDaniel Vetter 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
121052217195SDaniel Vetter 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
121152217195SDaniel Vetter 
121252217195SDaniel Vetter 	return 0;
121352217195SDaniel Vetter }
121452217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
121552217195SDaniel Vetter 
121652217195SDaniel Vetter /**
121750525c33SStanislav Lisovskiy  * DOC: HDMI connector properties
121850525c33SStanislav Lisovskiy  *
121950525c33SStanislav Lisovskiy  * content type (HDMI specific):
122050525c33SStanislav Lisovskiy  *	Indicates content type setting to be used in HDMI infoframes to indicate
12211e55a53aSMatt Roper  *	content type for the external device, so that it adjusts its display
122250525c33SStanislav Lisovskiy  *	settings accordingly.
122350525c33SStanislav Lisovskiy  *
122450525c33SStanislav Lisovskiy  *	The value of this property can be one of the following:
122550525c33SStanislav Lisovskiy  *
122650525c33SStanislav Lisovskiy  *	No Data:
122750525c33SStanislav Lisovskiy  *		Content type is unknown
122850525c33SStanislav Lisovskiy  *	Graphics:
122950525c33SStanislav Lisovskiy  *		Content type is graphics
123050525c33SStanislav Lisovskiy  *	Photo:
123150525c33SStanislav Lisovskiy  *		Content type is photo
123250525c33SStanislav Lisovskiy  *	Cinema:
123350525c33SStanislav Lisovskiy  *		Content type is cinema
123450525c33SStanislav Lisovskiy  *	Game:
123550525c33SStanislav Lisovskiy  *		Content type is game
123650525c33SStanislav Lisovskiy  *
123750525c33SStanislav Lisovskiy  *	Drivers can set up this property by calling
123850525c33SStanislav Lisovskiy  *	drm_connector_attach_content_type_property(). Decoding to
1239ba609631SDaniel Vetter  *	infoframe values is done through drm_hdmi_avi_infoframe_content_type().
124050525c33SStanislav Lisovskiy  */
124150525c33SStanislav Lisovskiy 
124250525c33SStanislav Lisovskiy /**
124350525c33SStanislav Lisovskiy  * drm_connector_attach_content_type_property - attach content-type property
124450525c33SStanislav Lisovskiy  * @connector: connector to attach content type property on.
124550525c33SStanislav Lisovskiy  *
124650525c33SStanislav Lisovskiy  * Called by a driver the first time a HDMI connector is made.
124750525c33SStanislav Lisovskiy  */
124850525c33SStanislav Lisovskiy int drm_connector_attach_content_type_property(struct drm_connector *connector)
124950525c33SStanislav Lisovskiy {
125050525c33SStanislav Lisovskiy 	if (!drm_mode_create_content_type_property(connector->dev))
125150525c33SStanislav Lisovskiy 		drm_object_attach_property(&connector->base,
125250525c33SStanislav Lisovskiy 					   connector->dev->mode_config.content_type_property,
125350525c33SStanislav Lisovskiy 					   DRM_MODE_CONTENT_TYPE_NO_DATA);
125450525c33SStanislav Lisovskiy 	return 0;
125550525c33SStanislav Lisovskiy }
125650525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_connector_attach_content_type_property);
125750525c33SStanislav Lisovskiy 
125850525c33SStanislav Lisovskiy 
125950525c33SStanislav Lisovskiy /**
126050525c33SStanislav Lisovskiy  * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
126150525c33SStanislav Lisovskiy  *                                         content type information, based
126250525c33SStanislav Lisovskiy  *                                         on correspondent DRM property.
126350525c33SStanislav Lisovskiy  * @frame: HDMI AVI infoframe
126450525c33SStanislav Lisovskiy  * @conn_state: DRM display connector state
126550525c33SStanislav Lisovskiy  *
126650525c33SStanislav Lisovskiy  */
126750525c33SStanislav Lisovskiy void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
126850525c33SStanislav Lisovskiy 					 const struct drm_connector_state *conn_state)
126950525c33SStanislav Lisovskiy {
127050525c33SStanislav Lisovskiy 	switch (conn_state->content_type) {
127150525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_GRAPHICS:
127250525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
127350525c33SStanislav Lisovskiy 		break;
127450525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_CINEMA:
127550525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
127650525c33SStanislav Lisovskiy 		break;
127750525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_GAME:
127850525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GAME;
127950525c33SStanislav Lisovskiy 		break;
128050525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_PHOTO:
128150525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
128250525c33SStanislav Lisovskiy 		break;
128350525c33SStanislav Lisovskiy 	default:
128450525c33SStanislav Lisovskiy 		/* Graphics is the default(0) */
128550525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
128650525c33SStanislav Lisovskiy 	}
128750525c33SStanislav Lisovskiy 
128850525c33SStanislav Lisovskiy 	frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
128950525c33SStanislav Lisovskiy }
129050525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
129150525c33SStanislav Lisovskiy 
129250525c33SStanislav Lisovskiy /**
12936c4f52dcSBoris Brezillon  * drm_mode_attach_tv_margin_properties - attach TV connector margin properties
12946c4f52dcSBoris Brezillon  * @connector: DRM connector
12956c4f52dcSBoris Brezillon  *
12966c4f52dcSBoris Brezillon  * Called by a driver when it needs to attach TV margin props to a connector.
12976c4f52dcSBoris Brezillon  * Typically used on SDTV and HDMI connectors.
12986c4f52dcSBoris Brezillon  */
12996c4f52dcSBoris Brezillon void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
13006c4f52dcSBoris Brezillon {
13016c4f52dcSBoris Brezillon 	struct drm_device *dev = connector->dev;
13026c4f52dcSBoris Brezillon 
13036c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
13046c4f52dcSBoris Brezillon 				   dev->mode_config.tv_left_margin_property,
13056c4f52dcSBoris Brezillon 				   0);
13066c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
13076c4f52dcSBoris Brezillon 				   dev->mode_config.tv_right_margin_property,
13086c4f52dcSBoris Brezillon 				   0);
13096c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
13106c4f52dcSBoris Brezillon 				   dev->mode_config.tv_top_margin_property,
13116c4f52dcSBoris Brezillon 				   0);
13126c4f52dcSBoris Brezillon 	drm_object_attach_property(&connector->base,
13136c4f52dcSBoris Brezillon 				   dev->mode_config.tv_bottom_margin_property,
13146c4f52dcSBoris Brezillon 				   0);
13156c4f52dcSBoris Brezillon }
13166c4f52dcSBoris Brezillon EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
13176c4f52dcSBoris Brezillon 
13186c4f52dcSBoris Brezillon /**
13196c4f52dcSBoris Brezillon  * drm_mode_create_tv_margin_properties - create TV connector margin properties
13206c4f52dcSBoris Brezillon  * @dev: DRM device
13216c4f52dcSBoris Brezillon  *
13226c4f52dcSBoris Brezillon  * Called by a driver's HDMI connector initialization routine, this function
13236c4f52dcSBoris Brezillon  * creates the TV margin properties for a given device. No need to call this
13246c4f52dcSBoris Brezillon  * function for an SDTV connector, it's already called from
13256c4f52dcSBoris Brezillon  * drm_mode_create_tv_properties().
13266c4f52dcSBoris Brezillon  */
13276c4f52dcSBoris Brezillon int drm_mode_create_tv_margin_properties(struct drm_device *dev)
13286c4f52dcSBoris Brezillon {
13296c4f52dcSBoris Brezillon 	if (dev->mode_config.tv_left_margin_property)
13306c4f52dcSBoris Brezillon 		return 0;
13316c4f52dcSBoris Brezillon 
13326c4f52dcSBoris Brezillon 	dev->mode_config.tv_left_margin_property =
13336c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "left margin", 0, 100);
13346c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_left_margin_property)
13356c4f52dcSBoris Brezillon 		return -ENOMEM;
13366c4f52dcSBoris Brezillon 
13376c4f52dcSBoris Brezillon 	dev->mode_config.tv_right_margin_property =
13386c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "right margin", 0, 100);
13396c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_right_margin_property)
13406c4f52dcSBoris Brezillon 		return -ENOMEM;
13416c4f52dcSBoris Brezillon 
13426c4f52dcSBoris Brezillon 	dev->mode_config.tv_top_margin_property =
13436c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "top margin", 0, 100);
13446c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_top_margin_property)
13456c4f52dcSBoris Brezillon 		return -ENOMEM;
13466c4f52dcSBoris Brezillon 
13476c4f52dcSBoris Brezillon 	dev->mode_config.tv_bottom_margin_property =
13486c4f52dcSBoris Brezillon 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
13496c4f52dcSBoris Brezillon 	if (!dev->mode_config.tv_bottom_margin_property)
13506c4f52dcSBoris Brezillon 		return -ENOMEM;
13516c4f52dcSBoris Brezillon 
13526c4f52dcSBoris Brezillon 	return 0;
13536c4f52dcSBoris Brezillon }
13546c4f52dcSBoris Brezillon EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
13556c4f52dcSBoris Brezillon 
13566c4f52dcSBoris Brezillon /**
1357eda6887fSBoris Brezillon  * drm_mode_create_tv_properties - create TV specific connector properties
135852217195SDaniel Vetter  * @dev: DRM device
135952217195SDaniel Vetter  * @num_modes: number of different TV formats (modes) supported
136052217195SDaniel Vetter  * @modes: array of pointers to strings containing name of each format
136152217195SDaniel Vetter  *
136252217195SDaniel Vetter  * Called by a driver's TV initialization routine, this function creates
136352217195SDaniel Vetter  * the TV specific connector properties for a given device.  Caller is
136452217195SDaniel Vetter  * responsible for allocating a list of format names and passing them to
136552217195SDaniel Vetter  * this routine.
136652217195SDaniel Vetter  */
136752217195SDaniel Vetter int drm_mode_create_tv_properties(struct drm_device *dev,
136852217195SDaniel Vetter 				  unsigned int num_modes,
136952217195SDaniel Vetter 				  const char * const modes[])
137052217195SDaniel Vetter {
137152217195SDaniel Vetter 	struct drm_property *tv_selector;
137252217195SDaniel Vetter 	struct drm_property *tv_subconnector;
137352217195SDaniel Vetter 	unsigned int i;
137452217195SDaniel Vetter 
137552217195SDaniel Vetter 	if (dev->mode_config.tv_select_subconnector_property)
137652217195SDaniel Vetter 		return 0;
137752217195SDaniel Vetter 
137852217195SDaniel Vetter 	/*
137952217195SDaniel Vetter 	 * Basic connector properties
138052217195SDaniel Vetter 	 */
138152217195SDaniel Vetter 	tv_selector = drm_property_create_enum(dev, 0,
138252217195SDaniel Vetter 					  "select subconnector",
138352217195SDaniel Vetter 					  drm_tv_select_enum_list,
138452217195SDaniel Vetter 					  ARRAY_SIZE(drm_tv_select_enum_list));
138552217195SDaniel Vetter 	if (!tv_selector)
138652217195SDaniel Vetter 		goto nomem;
138752217195SDaniel Vetter 
138852217195SDaniel Vetter 	dev->mode_config.tv_select_subconnector_property = tv_selector;
138952217195SDaniel Vetter 
139052217195SDaniel Vetter 	tv_subconnector =
139152217195SDaniel Vetter 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
139252217195SDaniel Vetter 				    "subconnector",
139352217195SDaniel Vetter 				    drm_tv_subconnector_enum_list,
139452217195SDaniel Vetter 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
139552217195SDaniel Vetter 	if (!tv_subconnector)
139652217195SDaniel Vetter 		goto nomem;
139752217195SDaniel Vetter 	dev->mode_config.tv_subconnector_property = tv_subconnector;
139852217195SDaniel Vetter 
139952217195SDaniel Vetter 	/*
140052217195SDaniel Vetter 	 * Other, TV specific properties: margins & TV modes.
140152217195SDaniel Vetter 	 */
14026c4f52dcSBoris Brezillon 	if (drm_mode_create_tv_margin_properties(dev))
140352217195SDaniel Vetter 		goto nomem;
140452217195SDaniel Vetter 
140552217195SDaniel Vetter 	dev->mode_config.tv_mode_property =
140652217195SDaniel Vetter 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
140752217195SDaniel Vetter 				    "mode", num_modes);
140852217195SDaniel Vetter 	if (!dev->mode_config.tv_mode_property)
140952217195SDaniel Vetter 		goto nomem;
141052217195SDaniel Vetter 
141152217195SDaniel Vetter 	for (i = 0; i < num_modes; i++)
141230e9db6dSVille Syrjälä 		drm_property_add_enum(dev->mode_config.tv_mode_property,
141352217195SDaniel Vetter 				      i, modes[i]);
141452217195SDaniel Vetter 
141552217195SDaniel Vetter 	dev->mode_config.tv_brightness_property =
141652217195SDaniel Vetter 		drm_property_create_range(dev, 0, "brightness", 0, 100);
141752217195SDaniel Vetter 	if (!dev->mode_config.tv_brightness_property)
141852217195SDaniel Vetter 		goto nomem;
141952217195SDaniel Vetter 
142052217195SDaniel Vetter 	dev->mode_config.tv_contrast_property =
142152217195SDaniel Vetter 		drm_property_create_range(dev, 0, "contrast", 0, 100);
142252217195SDaniel Vetter 	if (!dev->mode_config.tv_contrast_property)
142352217195SDaniel Vetter 		goto nomem;
142452217195SDaniel Vetter 
142552217195SDaniel Vetter 	dev->mode_config.tv_flicker_reduction_property =
142652217195SDaniel Vetter 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
142752217195SDaniel Vetter 	if (!dev->mode_config.tv_flicker_reduction_property)
142852217195SDaniel Vetter 		goto nomem;
142952217195SDaniel Vetter 
143052217195SDaniel Vetter 	dev->mode_config.tv_overscan_property =
143152217195SDaniel Vetter 		drm_property_create_range(dev, 0, "overscan", 0, 100);
143252217195SDaniel Vetter 	if (!dev->mode_config.tv_overscan_property)
143352217195SDaniel Vetter 		goto nomem;
143452217195SDaniel Vetter 
143552217195SDaniel Vetter 	dev->mode_config.tv_saturation_property =
143652217195SDaniel Vetter 		drm_property_create_range(dev, 0, "saturation", 0, 100);
143752217195SDaniel Vetter 	if (!dev->mode_config.tv_saturation_property)
143852217195SDaniel Vetter 		goto nomem;
143952217195SDaniel Vetter 
144052217195SDaniel Vetter 	dev->mode_config.tv_hue_property =
144152217195SDaniel Vetter 		drm_property_create_range(dev, 0, "hue", 0, 100);
144252217195SDaniel Vetter 	if (!dev->mode_config.tv_hue_property)
144352217195SDaniel Vetter 		goto nomem;
144452217195SDaniel Vetter 
144552217195SDaniel Vetter 	return 0;
144652217195SDaniel Vetter nomem:
144752217195SDaniel Vetter 	return -ENOMEM;
144852217195SDaniel Vetter }
144952217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_tv_properties);
145052217195SDaniel Vetter 
145152217195SDaniel Vetter /**
145252217195SDaniel Vetter  * drm_mode_create_scaling_mode_property - create scaling mode property
145352217195SDaniel Vetter  * @dev: DRM device
145452217195SDaniel Vetter  *
145552217195SDaniel Vetter  * Called by a driver the first time it's needed, must be attached to desired
145652217195SDaniel Vetter  * connectors.
14578f6e1e22SMaarten Lankhorst  *
14588f6e1e22SMaarten Lankhorst  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
14598f6e1e22SMaarten Lankhorst  * instead to correctly assign &drm_connector_state.picture_aspect_ratio
14608f6e1e22SMaarten Lankhorst  * in the atomic state.
146152217195SDaniel Vetter  */
146252217195SDaniel Vetter int drm_mode_create_scaling_mode_property(struct drm_device *dev)
146352217195SDaniel Vetter {
146452217195SDaniel Vetter 	struct drm_property *scaling_mode;
146552217195SDaniel Vetter 
146652217195SDaniel Vetter 	if (dev->mode_config.scaling_mode_property)
146752217195SDaniel Vetter 		return 0;
146852217195SDaniel Vetter 
146952217195SDaniel Vetter 	scaling_mode =
147052217195SDaniel Vetter 		drm_property_create_enum(dev, 0, "scaling mode",
147152217195SDaniel Vetter 				drm_scaling_mode_enum_list,
147252217195SDaniel Vetter 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
147352217195SDaniel Vetter 
147452217195SDaniel Vetter 	dev->mode_config.scaling_mode_property = scaling_mode;
147552217195SDaniel Vetter 
147652217195SDaniel Vetter 	return 0;
147752217195SDaniel Vetter }
147852217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
147952217195SDaniel Vetter 
148052217195SDaniel Vetter /**
1481ab7a664fSNicholas Kazlauskas  * DOC: Variable refresh properties
1482ab7a664fSNicholas Kazlauskas  *
1483ab7a664fSNicholas Kazlauskas  * Variable refresh rate capable displays can dynamically adjust their
1484ab7a664fSNicholas Kazlauskas  * refresh rate by extending the duration of their vertical front porch
1485ab7a664fSNicholas Kazlauskas  * until page flip or timeout occurs. This can reduce or remove stuttering
1486ab7a664fSNicholas Kazlauskas  * and latency in scenarios where the page flip does not align with the
1487ab7a664fSNicholas Kazlauskas  * vblank interval.
1488ab7a664fSNicholas Kazlauskas  *
1489ab7a664fSNicholas Kazlauskas  * An example scenario would be an application flipping at a constant rate
1490ab7a664fSNicholas Kazlauskas  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
1491ab7a664fSNicholas Kazlauskas  * interval and the same contents will be displayed twice. This can be
1492ab7a664fSNicholas Kazlauskas  * observed as stuttering for content with motion.
1493ab7a664fSNicholas Kazlauskas  *
1494ab7a664fSNicholas Kazlauskas  * If variable refresh rate was active on a display that supported a
1495ab7a664fSNicholas Kazlauskas  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
1496ab7a664fSNicholas Kazlauskas  * for the example scenario. The minimum supported variable refresh rate of
1497ab7a664fSNicholas Kazlauskas  * 35Hz is below the page flip frequency and the vertical front porch can
1498ab7a664fSNicholas Kazlauskas  * be extended until the page flip occurs. The vblank interval will be
1499ab7a664fSNicholas Kazlauskas  * directly aligned to the page flip rate.
1500ab7a664fSNicholas Kazlauskas  *
1501ab7a664fSNicholas Kazlauskas  * Not all userspace content is suitable for use with variable refresh rate.
1502ab7a664fSNicholas Kazlauskas  * Large and frequent changes in vertical front porch duration may worsen
1503ab7a664fSNicholas Kazlauskas  * perceived stuttering for input sensitive applications.
1504ab7a664fSNicholas Kazlauskas  *
1505ab7a664fSNicholas Kazlauskas  * Panel brightness will also vary with vertical front porch duration. Some
1506ab7a664fSNicholas Kazlauskas  * panels may have noticeable differences in brightness between the minimum
1507ab7a664fSNicholas Kazlauskas  * vertical front porch duration and the maximum vertical front porch duration.
1508ab7a664fSNicholas Kazlauskas  * Large and frequent changes in vertical front porch duration may produce
1509ab7a664fSNicholas Kazlauskas  * observable flickering for such panels.
1510ab7a664fSNicholas Kazlauskas  *
1511ab7a664fSNicholas Kazlauskas  * Userspace control for variable refresh rate is supported via properties
1512ab7a664fSNicholas Kazlauskas  * on the &drm_connector and &drm_crtc objects.
1513ab7a664fSNicholas Kazlauskas  *
1514ab7a664fSNicholas Kazlauskas  * "vrr_capable":
1515ab7a664fSNicholas Kazlauskas  *	Optional &drm_connector boolean property that drivers should attach
1516ab7a664fSNicholas Kazlauskas  *	with drm_connector_attach_vrr_capable_property() on connectors that
1517ab7a664fSNicholas Kazlauskas  *	could support variable refresh rates. Drivers should update the
1518ab7a664fSNicholas Kazlauskas  *	property value by calling drm_connector_set_vrr_capable_property().
1519ab7a664fSNicholas Kazlauskas  *
1520ab7a664fSNicholas Kazlauskas  *	Absence of the property should indicate absence of support.
1521ab7a664fSNicholas Kazlauskas  *
152277086014SDaniel Vetter  * "VRR_ENABLED":
1523ab7a664fSNicholas Kazlauskas  *	Default &drm_crtc boolean property that notifies the driver that the
1524ab7a664fSNicholas Kazlauskas  *	content on the CRTC is suitable for variable refresh rate presentation.
1525ab7a664fSNicholas Kazlauskas  *	The driver will take this property as a hint to enable variable
1526ab7a664fSNicholas Kazlauskas  *	refresh rate support if the receiver supports it, ie. if the
1527ab7a664fSNicholas Kazlauskas  *	"vrr_capable" property is true on the &drm_connector object. The
1528ab7a664fSNicholas Kazlauskas  *	vertical front porch duration will be extended until page-flip or
1529ab7a664fSNicholas Kazlauskas  *	timeout when enabled.
1530ab7a664fSNicholas Kazlauskas  *
1531ab7a664fSNicholas Kazlauskas  *	The minimum vertical front porch duration is defined as the vertical
1532ab7a664fSNicholas Kazlauskas  *	front porch duration for the current mode.
1533ab7a664fSNicholas Kazlauskas  *
1534ab7a664fSNicholas Kazlauskas  *	The maximum vertical front porch duration is greater than or equal to
1535ab7a664fSNicholas Kazlauskas  *	the minimum vertical front porch duration. The duration is derived
1536ab7a664fSNicholas Kazlauskas  *	from the minimum supported variable refresh rate for the connector.
1537ab7a664fSNicholas Kazlauskas  *
1538ab7a664fSNicholas Kazlauskas  *	The driver may place further restrictions within these minimum
1539ab7a664fSNicholas Kazlauskas  *	and maximum bounds.
1540ab7a664fSNicholas Kazlauskas  */
1541ab7a664fSNicholas Kazlauskas 
1542ab7a664fSNicholas Kazlauskas /**
1543ba1b0f6cSNicholas Kazlauskas  * drm_connector_attach_vrr_capable_property - creates the
1544ba1b0f6cSNicholas Kazlauskas  * vrr_capable property
1545ba1b0f6cSNicholas Kazlauskas  * @connector: connector to create the vrr_capable property on.
1546ba1b0f6cSNicholas Kazlauskas  *
1547ba1b0f6cSNicholas Kazlauskas  * This is used by atomic drivers to add support for querying
1548ba1b0f6cSNicholas Kazlauskas  * variable refresh rate capability for a connector.
1549ba1b0f6cSNicholas Kazlauskas  *
1550ba1b0f6cSNicholas Kazlauskas  * Returns:
1551ba1b0f6cSNicholas Kazlauskas  * Zero on success, negative errono on failure.
1552ba1b0f6cSNicholas Kazlauskas  */
1553ba1b0f6cSNicholas Kazlauskas int drm_connector_attach_vrr_capable_property(
1554ba1b0f6cSNicholas Kazlauskas 	struct drm_connector *connector)
1555ba1b0f6cSNicholas Kazlauskas {
1556ba1b0f6cSNicholas Kazlauskas 	struct drm_device *dev = connector->dev;
1557ba1b0f6cSNicholas Kazlauskas 	struct drm_property *prop;
1558ba1b0f6cSNicholas Kazlauskas 
1559ba1b0f6cSNicholas Kazlauskas 	if (!connector->vrr_capable_property) {
1560ba1b0f6cSNicholas Kazlauskas 		prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
1561ba1b0f6cSNicholas Kazlauskas 			"vrr_capable");
1562ba1b0f6cSNicholas Kazlauskas 		if (!prop)
1563ba1b0f6cSNicholas Kazlauskas 			return -ENOMEM;
1564ba1b0f6cSNicholas Kazlauskas 
1565ba1b0f6cSNicholas Kazlauskas 		connector->vrr_capable_property = prop;
1566ba1b0f6cSNicholas Kazlauskas 		drm_object_attach_property(&connector->base, prop, 0);
1567ba1b0f6cSNicholas Kazlauskas 	}
1568ba1b0f6cSNicholas Kazlauskas 
1569ba1b0f6cSNicholas Kazlauskas 	return 0;
1570ba1b0f6cSNicholas Kazlauskas }
1571ba1b0f6cSNicholas Kazlauskas EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
1572ba1b0f6cSNicholas Kazlauskas 
1573ba1b0f6cSNicholas Kazlauskas /**
15748f6e1e22SMaarten Lankhorst  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
15758f6e1e22SMaarten Lankhorst  * @connector: connector to attach scaling mode property on.
15768f6e1e22SMaarten Lankhorst  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
15778f6e1e22SMaarten Lankhorst  *
15788f6e1e22SMaarten Lankhorst  * This is used to add support for scaling mode to atomic drivers.
15798f6e1e22SMaarten Lankhorst  * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
15808f6e1e22SMaarten Lankhorst  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
15818f6e1e22SMaarten Lankhorst  *
15828f6e1e22SMaarten Lankhorst  * This is the atomic version of drm_mode_create_scaling_mode_property().
15838f6e1e22SMaarten Lankhorst  *
15848f6e1e22SMaarten Lankhorst  * Returns:
15858f6e1e22SMaarten Lankhorst  * Zero on success, negative errno on failure.
15868f6e1e22SMaarten Lankhorst  */
15878f6e1e22SMaarten Lankhorst int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
15888f6e1e22SMaarten Lankhorst 					       u32 scaling_mode_mask)
15898f6e1e22SMaarten Lankhorst {
15908f6e1e22SMaarten Lankhorst 	struct drm_device *dev = connector->dev;
15918f6e1e22SMaarten Lankhorst 	struct drm_property *scaling_mode_property;
159230e9db6dSVille Syrjälä 	int i;
15938f6e1e22SMaarten Lankhorst 	const unsigned valid_scaling_mode_mask =
15948f6e1e22SMaarten Lankhorst 		(1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
15958f6e1e22SMaarten Lankhorst 
15968f6e1e22SMaarten Lankhorst 	if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
15978f6e1e22SMaarten Lankhorst 		    scaling_mode_mask & ~valid_scaling_mode_mask))
15988f6e1e22SMaarten Lankhorst 		return -EINVAL;
15998f6e1e22SMaarten Lankhorst 
16008f6e1e22SMaarten Lankhorst 	scaling_mode_property =
16018f6e1e22SMaarten Lankhorst 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
16028f6e1e22SMaarten Lankhorst 				    hweight32(scaling_mode_mask));
16038f6e1e22SMaarten Lankhorst 
16048f6e1e22SMaarten Lankhorst 	if (!scaling_mode_property)
16058f6e1e22SMaarten Lankhorst 		return -ENOMEM;
16068f6e1e22SMaarten Lankhorst 
16078f6e1e22SMaarten Lankhorst 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
16088f6e1e22SMaarten Lankhorst 		int ret;
16098f6e1e22SMaarten Lankhorst 
16108f6e1e22SMaarten Lankhorst 		if (!(BIT(i) & scaling_mode_mask))
16118f6e1e22SMaarten Lankhorst 			continue;
16128f6e1e22SMaarten Lankhorst 
161330e9db6dSVille Syrjälä 		ret = drm_property_add_enum(scaling_mode_property,
16148f6e1e22SMaarten Lankhorst 					    drm_scaling_mode_enum_list[i].type,
16158f6e1e22SMaarten Lankhorst 					    drm_scaling_mode_enum_list[i].name);
16168f6e1e22SMaarten Lankhorst 
16178f6e1e22SMaarten Lankhorst 		if (ret) {
16188f6e1e22SMaarten Lankhorst 			drm_property_destroy(dev, scaling_mode_property);
16198f6e1e22SMaarten Lankhorst 
16208f6e1e22SMaarten Lankhorst 			return ret;
16218f6e1e22SMaarten Lankhorst 		}
16228f6e1e22SMaarten Lankhorst 	}
16238f6e1e22SMaarten Lankhorst 
16248f6e1e22SMaarten Lankhorst 	drm_object_attach_property(&connector->base,
16258f6e1e22SMaarten Lankhorst 				   scaling_mode_property, 0);
16268f6e1e22SMaarten Lankhorst 
16278f6e1e22SMaarten Lankhorst 	connector->scaling_mode_property = scaling_mode_property;
16288f6e1e22SMaarten Lankhorst 
16298f6e1e22SMaarten Lankhorst 	return 0;
16308f6e1e22SMaarten Lankhorst }
16318f6e1e22SMaarten Lankhorst EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
16328f6e1e22SMaarten Lankhorst 
16338f6e1e22SMaarten Lankhorst /**
163452217195SDaniel Vetter  * drm_mode_create_aspect_ratio_property - create aspect ratio property
163552217195SDaniel Vetter  * @dev: DRM device
163652217195SDaniel Vetter  *
163752217195SDaniel Vetter  * Called by a driver the first time it's needed, must be attached to desired
163852217195SDaniel Vetter  * connectors.
163952217195SDaniel Vetter  *
164052217195SDaniel Vetter  * Returns:
164152217195SDaniel Vetter  * Zero on success, negative errno on failure.
164252217195SDaniel Vetter  */
164352217195SDaniel Vetter int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
164452217195SDaniel Vetter {
164552217195SDaniel Vetter 	if (dev->mode_config.aspect_ratio_property)
164652217195SDaniel Vetter 		return 0;
164752217195SDaniel Vetter 
164852217195SDaniel Vetter 	dev->mode_config.aspect_ratio_property =
164952217195SDaniel Vetter 		drm_property_create_enum(dev, 0, "aspect ratio",
165052217195SDaniel Vetter 				drm_aspect_ratio_enum_list,
165152217195SDaniel Vetter 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
165252217195SDaniel Vetter 
165352217195SDaniel Vetter 	if (dev->mode_config.aspect_ratio_property == NULL)
165452217195SDaniel Vetter 		return -ENOMEM;
165552217195SDaniel Vetter 
165652217195SDaniel Vetter 	return 0;
165752217195SDaniel Vetter }
165852217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
165952217195SDaniel Vetter 
166052217195SDaniel Vetter /**
1661d2c6a405SUma Shankar  * DOC: standard connector properties
1662d2c6a405SUma Shankar  *
1663d2c6a405SUma Shankar  * Colorspace:
1664d2c6a405SUma Shankar  *     drm_mode_create_colorspace_property - create colorspace property
1665d2c6a405SUma Shankar  *     This property helps select a suitable colorspace based on the sink
1666d2c6a405SUma Shankar  *     capability. Modern sink devices support wider gamut like BT2020.
1667d2c6a405SUma Shankar  *     This helps switch to BT2020 mode if the BT2020 encoded video stream
1668d2c6a405SUma Shankar  *     is being played by the user, same for any other colorspace. Thereby
1669d2c6a405SUma Shankar  *     giving a good visual experience to users.
1670d2c6a405SUma Shankar  *
1671d2c6a405SUma Shankar  *     The expectation from userspace is that it should parse the EDID
1672d2c6a405SUma Shankar  *     and get supported colorspaces. Use this property and switch to the
1673d2c6a405SUma Shankar  *     one supported. Sink supported colorspaces should be retrieved by
1674d2c6a405SUma Shankar  *     userspace from EDID and driver will not explicitly expose them.
1675d2c6a405SUma Shankar  *
1676d2c6a405SUma Shankar  *     Basically the expectation from userspace is:
1677d2c6a405SUma Shankar  *      - Set up CRTC DEGAMMA/CTM/GAMMA to convert to some sink
1678d2c6a405SUma Shankar  *        colorspace
1679d2c6a405SUma Shankar  *      - Set this new property to let the sink know what it
1680d2c6a405SUma Shankar  *        converted the CRTC output to.
1681d2c6a405SUma Shankar  *      - This property is just to inform sink what colorspace
1682d2c6a405SUma Shankar  *        source is trying to drive.
1683d2c6a405SUma Shankar  *
1684d2c6a405SUma Shankar  * Called by a driver the first time it's needed, must be attached to desired
1685d2c6a405SUma Shankar  * connectors.
1686d2c6a405SUma Shankar  */
1687d2c6a405SUma Shankar int drm_mode_create_colorspace_property(struct drm_connector *connector)
1688d2c6a405SUma Shankar {
1689d2c6a405SUma Shankar 	struct drm_device *dev = connector->dev;
1690d2c6a405SUma Shankar 	struct drm_property *prop;
1691d2c6a405SUma Shankar 
1692d2c6a405SUma Shankar 	if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
1693d2c6a405SUma Shankar 	    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
1694d2c6a405SUma Shankar 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
1695d2c6a405SUma Shankar 						"Colorspace",
1696d2c6a405SUma Shankar 						hdmi_colorspaces,
1697d2c6a405SUma Shankar 						ARRAY_SIZE(hdmi_colorspaces));
1698d2c6a405SUma Shankar 		if (!prop)
1699d2c6a405SUma Shankar 			return -ENOMEM;
1700d2c6a405SUma Shankar 	} else {
1701d2c6a405SUma Shankar 		DRM_DEBUG_KMS("Colorspace property not supported\n");
1702d2c6a405SUma Shankar 		return 0;
1703d2c6a405SUma Shankar 	}
1704d2c6a405SUma Shankar 
1705d2c6a405SUma Shankar 	connector->colorspace_property = prop;
1706d2c6a405SUma Shankar 
1707d2c6a405SUma Shankar 	return 0;
1708d2c6a405SUma Shankar }
1709d2c6a405SUma Shankar EXPORT_SYMBOL(drm_mode_create_colorspace_property);
1710d2c6a405SUma Shankar 
1711d2c6a405SUma Shankar /**
171250525c33SStanislav Lisovskiy  * drm_mode_create_content_type_property - create content type property
171350525c33SStanislav Lisovskiy  * @dev: DRM device
171450525c33SStanislav Lisovskiy  *
171550525c33SStanislav Lisovskiy  * Called by a driver the first time it's needed, must be attached to desired
171650525c33SStanislav Lisovskiy  * connectors.
171750525c33SStanislav Lisovskiy  *
171850525c33SStanislav Lisovskiy  * Returns:
171950525c33SStanislav Lisovskiy  * Zero on success, negative errno on failure.
172050525c33SStanislav Lisovskiy  */
172150525c33SStanislav Lisovskiy int drm_mode_create_content_type_property(struct drm_device *dev)
172250525c33SStanislav Lisovskiy {
172350525c33SStanislav Lisovskiy 	if (dev->mode_config.content_type_property)
172450525c33SStanislav Lisovskiy 		return 0;
172550525c33SStanislav Lisovskiy 
172650525c33SStanislav Lisovskiy 	dev->mode_config.content_type_property =
172750525c33SStanislav Lisovskiy 		drm_property_create_enum(dev, 0, "content type",
172850525c33SStanislav Lisovskiy 					 drm_content_type_enum_list,
172950525c33SStanislav Lisovskiy 					 ARRAY_SIZE(drm_content_type_enum_list));
173050525c33SStanislav Lisovskiy 
173150525c33SStanislav Lisovskiy 	if (dev->mode_config.content_type_property == NULL)
173250525c33SStanislav Lisovskiy 		return -ENOMEM;
173350525c33SStanislav Lisovskiy 
173450525c33SStanislav Lisovskiy 	return 0;
173550525c33SStanislav Lisovskiy }
173650525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_mode_create_content_type_property);
173750525c33SStanislav Lisovskiy 
173850525c33SStanislav Lisovskiy /**
173952217195SDaniel Vetter  * drm_mode_create_suggested_offset_properties - create suggests offset properties
174052217195SDaniel Vetter  * @dev: DRM device
174152217195SDaniel Vetter  *
174252217195SDaniel Vetter  * Create the the suggested x/y offset property for connectors.
174352217195SDaniel Vetter  */
174452217195SDaniel Vetter int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
174552217195SDaniel Vetter {
174652217195SDaniel Vetter 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
174752217195SDaniel Vetter 		return 0;
174852217195SDaniel Vetter 
174952217195SDaniel Vetter 	dev->mode_config.suggested_x_property =
175052217195SDaniel Vetter 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
175152217195SDaniel Vetter 
175252217195SDaniel Vetter 	dev->mode_config.suggested_y_property =
175352217195SDaniel Vetter 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
175452217195SDaniel Vetter 
175552217195SDaniel Vetter 	if (dev->mode_config.suggested_x_property == NULL ||
175652217195SDaniel Vetter 	    dev->mode_config.suggested_y_property == NULL)
175752217195SDaniel Vetter 		return -ENOMEM;
175852217195SDaniel Vetter 	return 0;
175952217195SDaniel Vetter }
176052217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
176152217195SDaniel Vetter 
176252217195SDaniel Vetter /**
176397e14fbeSDaniel Vetter  * drm_connector_set_path_property - set tile property on connector
176452217195SDaniel Vetter  * @connector: connector to set property on.
176552217195SDaniel Vetter  * @path: path to use for property; must not be NULL.
176652217195SDaniel Vetter  *
176752217195SDaniel Vetter  * This creates a property to expose to userspace to specify a
176852217195SDaniel Vetter  * connector path. This is mainly used for DisplayPort MST where
176952217195SDaniel Vetter  * connectors have a topology and we want to allow userspace to give
177052217195SDaniel Vetter  * them more meaningful names.
177152217195SDaniel Vetter  *
177252217195SDaniel Vetter  * Returns:
177352217195SDaniel Vetter  * Zero on success, negative errno on failure.
177452217195SDaniel Vetter  */
177597e14fbeSDaniel Vetter int drm_connector_set_path_property(struct drm_connector *connector,
177652217195SDaniel Vetter 				    const char *path)
177752217195SDaniel Vetter {
177852217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
177952217195SDaniel Vetter 	int ret;
178052217195SDaniel Vetter 
178152217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
178252217195SDaniel Vetter 	                                       &connector->path_blob_ptr,
178352217195SDaniel Vetter 	                                       strlen(path) + 1,
178452217195SDaniel Vetter 	                                       path,
178552217195SDaniel Vetter 	                                       &connector->base,
178652217195SDaniel Vetter 	                                       dev->mode_config.path_property);
178752217195SDaniel Vetter 	return ret;
178852217195SDaniel Vetter }
178997e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_path_property);
179052217195SDaniel Vetter 
179152217195SDaniel Vetter /**
179297e14fbeSDaniel Vetter  * drm_connector_set_tile_property - set tile property on connector
179352217195SDaniel Vetter  * @connector: connector to set property on.
179452217195SDaniel Vetter  *
179552217195SDaniel Vetter  * This looks up the tile information for a connector, and creates a
179652217195SDaniel Vetter  * property for userspace to parse if it exists. The property is of
179752217195SDaniel Vetter  * the form of 8 integers using ':' as a separator.
17982de3a078SManasi Navare  * This is used for dual port tiled displays with DisplayPort SST
17992de3a078SManasi Navare  * or DisplayPort MST connectors.
180052217195SDaniel Vetter  *
180152217195SDaniel Vetter  * Returns:
180252217195SDaniel Vetter  * Zero on success, errno on failure.
180352217195SDaniel Vetter  */
180497e14fbeSDaniel Vetter int drm_connector_set_tile_property(struct drm_connector *connector)
180552217195SDaniel Vetter {
180652217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
180752217195SDaniel Vetter 	char tile[256];
180852217195SDaniel Vetter 	int ret;
180952217195SDaniel Vetter 
181052217195SDaniel Vetter 	if (!connector->has_tile) {
181152217195SDaniel Vetter 		ret  = drm_property_replace_global_blob(dev,
181252217195SDaniel Vetter 		                                        &connector->tile_blob_ptr,
181352217195SDaniel Vetter 		                                        0,
181452217195SDaniel Vetter 		                                        NULL,
181552217195SDaniel Vetter 		                                        &connector->base,
181652217195SDaniel Vetter 		                                        dev->mode_config.tile_property);
181752217195SDaniel Vetter 		return ret;
181852217195SDaniel Vetter 	}
181952217195SDaniel Vetter 
182052217195SDaniel Vetter 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
182152217195SDaniel Vetter 		 connector->tile_group->id, connector->tile_is_single_monitor,
182252217195SDaniel Vetter 		 connector->num_h_tile, connector->num_v_tile,
182352217195SDaniel Vetter 		 connector->tile_h_loc, connector->tile_v_loc,
182452217195SDaniel Vetter 		 connector->tile_h_size, connector->tile_v_size);
182552217195SDaniel Vetter 
182652217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
182752217195SDaniel Vetter 	                                       &connector->tile_blob_ptr,
182852217195SDaniel Vetter 	                                       strlen(tile) + 1,
182952217195SDaniel Vetter 	                                       tile,
183052217195SDaniel Vetter 	                                       &connector->base,
183152217195SDaniel Vetter 	                                       dev->mode_config.tile_property);
183252217195SDaniel Vetter 	return ret;
183352217195SDaniel Vetter }
183497e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_tile_property);
183552217195SDaniel Vetter 
183652217195SDaniel Vetter /**
1837c555f023SDaniel Vetter  * drm_connector_update_edid_property - update the edid property of a connector
183852217195SDaniel Vetter  * @connector: drm connector
183952217195SDaniel Vetter  * @edid: new value of the edid property
184052217195SDaniel Vetter  *
184152217195SDaniel Vetter  * This function creates a new blob modeset object and assigns its id to the
184252217195SDaniel Vetter  * connector's edid property.
18432de3a078SManasi Navare  * Since we also parse tile information from EDID's displayID block, we also
18442de3a078SManasi Navare  * set the connector's tile property here. See drm_connector_set_tile_property()
18452de3a078SManasi Navare  * for more details.
184652217195SDaniel Vetter  *
184752217195SDaniel Vetter  * Returns:
184852217195SDaniel Vetter  * Zero on success, negative errno on failure.
184952217195SDaniel Vetter  */
1850c555f023SDaniel Vetter int drm_connector_update_edid_property(struct drm_connector *connector,
185152217195SDaniel Vetter 				       const struct edid *edid)
185252217195SDaniel Vetter {
185352217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
185452217195SDaniel Vetter 	size_t size = 0;
185552217195SDaniel Vetter 	int ret;
185652217195SDaniel Vetter 
185752217195SDaniel Vetter 	/* ignore requests to set edid when overridden */
185852217195SDaniel Vetter 	if (connector->override_edid)
185952217195SDaniel Vetter 		return 0;
186052217195SDaniel Vetter 
186152217195SDaniel Vetter 	if (edid)
186252217195SDaniel Vetter 		size = EDID_LENGTH * (1 + edid->extensions);
186352217195SDaniel Vetter 
1864170178feSKeith Packard 	/* Set the display info, using edid if available, otherwise
1865170178feSKeith Packard 	 * reseting the values to defaults. This duplicates the work
1866170178feSKeith Packard 	 * done in drm_add_edid_modes, but that function is not
1867170178feSKeith Packard 	 * consistently called before this one in all drivers and the
1868170178feSKeith Packard 	 * computation is cheap enough that it seems better to
1869170178feSKeith Packard 	 * duplicate it rather than attempt to ensure some arbitrary
1870170178feSKeith Packard 	 * ordering of calls.
1871170178feSKeith Packard 	 */
1872170178feSKeith Packard 	if (edid)
1873170178feSKeith Packard 		drm_add_display_info(connector, edid);
1874170178feSKeith Packard 	else
1875170178feSKeith Packard 		drm_reset_display_info(connector);
1876170178feSKeith Packard 
187766660d4cSDave Airlie 	drm_object_property_set_value(&connector->base,
187866660d4cSDave Airlie 				      dev->mode_config.non_desktop_property,
187966660d4cSDave Airlie 				      connector->display_info.non_desktop);
188066660d4cSDave Airlie 
188152217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
188252217195SDaniel Vetter 					       &connector->edid_blob_ptr,
188352217195SDaniel Vetter 	                                       size,
188452217195SDaniel Vetter 	                                       edid,
188552217195SDaniel Vetter 	                                       &connector->base,
188652217195SDaniel Vetter 	                                       dev->mode_config.edid_property);
18872de3a078SManasi Navare 	if (ret)
188852217195SDaniel Vetter 		return ret;
18892de3a078SManasi Navare 	return drm_connector_set_tile_property(connector);
189052217195SDaniel Vetter }
1891c555f023SDaniel Vetter EXPORT_SYMBOL(drm_connector_update_edid_property);
189252217195SDaniel Vetter 
189340ee6fbeSManasi Navare /**
189497e14fbeSDaniel Vetter  * drm_connector_set_link_status_property - Set link status property of a connector
189540ee6fbeSManasi Navare  * @connector: drm connector
189640ee6fbeSManasi Navare  * @link_status: new value of link status property (0: Good, 1: Bad)
189740ee6fbeSManasi Navare  *
189840ee6fbeSManasi Navare  * In usual working scenario, this link status property will always be set to
189940ee6fbeSManasi Navare  * "GOOD". If something fails during or after a mode set, the kernel driver
190040ee6fbeSManasi Navare  * may set this link status property to "BAD". The caller then needs to send a
190140ee6fbeSManasi Navare  * hotplug uevent for userspace to re-check the valid modes through
190240ee6fbeSManasi Navare  * GET_CONNECTOR_IOCTL and retry modeset.
190340ee6fbeSManasi Navare  *
190440ee6fbeSManasi Navare  * Note: Drivers cannot rely on userspace to support this property and
190540ee6fbeSManasi Navare  * issue a modeset. As such, they may choose to handle issues (like
190640ee6fbeSManasi Navare  * re-training a link) without userspace's intervention.
190740ee6fbeSManasi Navare  *
190840ee6fbeSManasi Navare  * The reason for adding this property is to handle link training failures, but
190940ee6fbeSManasi Navare  * it is not limited to DP or link training. For example, if we implement
191040ee6fbeSManasi Navare  * asynchronous setcrtc, this property can be used to report any failures in that.
191140ee6fbeSManasi Navare  */
191297e14fbeSDaniel Vetter void drm_connector_set_link_status_property(struct drm_connector *connector,
191340ee6fbeSManasi Navare 					    uint64_t link_status)
191440ee6fbeSManasi Navare {
191540ee6fbeSManasi Navare 	struct drm_device *dev = connector->dev;
191640ee6fbeSManasi Navare 
191740ee6fbeSManasi Navare 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
191840ee6fbeSManasi Navare 	connector->state->link_status = link_status;
191940ee6fbeSManasi Navare 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
192040ee6fbeSManasi Navare }
192197e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_link_status_property);
192240ee6fbeSManasi Navare 
19238d70f395SHans de Goede /**
192447e22ff1SRadhakrishna Sripada  * drm_connector_attach_max_bpc_property - attach "max bpc" property
192547e22ff1SRadhakrishna Sripada  * @connector: connector to attach max bpc property on.
192647e22ff1SRadhakrishna Sripada  * @min: The minimum bit depth supported by the connector.
192747e22ff1SRadhakrishna Sripada  * @max: The maximum bit depth supported by the connector.
192847e22ff1SRadhakrishna Sripada  *
192947e22ff1SRadhakrishna Sripada  * This is used to add support for limiting the bit depth on a connector.
193047e22ff1SRadhakrishna Sripada  *
193147e22ff1SRadhakrishna Sripada  * Returns:
193247e22ff1SRadhakrishna Sripada  * Zero on success, negative errno on failure.
193347e22ff1SRadhakrishna Sripada  */
193447e22ff1SRadhakrishna Sripada int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
193547e22ff1SRadhakrishna Sripada 					  int min, int max)
193647e22ff1SRadhakrishna Sripada {
193747e22ff1SRadhakrishna Sripada 	struct drm_device *dev = connector->dev;
193847e22ff1SRadhakrishna Sripada 	struct drm_property *prop;
193947e22ff1SRadhakrishna Sripada 
194047e22ff1SRadhakrishna Sripada 	prop = connector->max_bpc_property;
194147e22ff1SRadhakrishna Sripada 	if (!prop) {
194247e22ff1SRadhakrishna Sripada 		prop = drm_property_create_range(dev, 0, "max bpc", min, max);
194347e22ff1SRadhakrishna Sripada 		if (!prop)
194447e22ff1SRadhakrishna Sripada 			return -ENOMEM;
194547e22ff1SRadhakrishna Sripada 
194647e22ff1SRadhakrishna Sripada 		connector->max_bpc_property = prop;
194747e22ff1SRadhakrishna Sripada 	}
194847e22ff1SRadhakrishna Sripada 
194947e22ff1SRadhakrishna Sripada 	drm_object_attach_property(&connector->base, prop, max);
195047e22ff1SRadhakrishna Sripada 	connector->state->max_requested_bpc = max;
195147e22ff1SRadhakrishna Sripada 	connector->state->max_bpc = max;
195247e22ff1SRadhakrishna Sripada 
195347e22ff1SRadhakrishna Sripada 	return 0;
195447e22ff1SRadhakrishna Sripada }
195547e22ff1SRadhakrishna Sripada EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
195647e22ff1SRadhakrishna Sripada 
195747e22ff1SRadhakrishna Sripada /**
1958ba1b0f6cSNicholas Kazlauskas  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
1959ba1b0f6cSNicholas Kazlauskas  * capable property for a connector
1960ba1b0f6cSNicholas Kazlauskas  * @connector: drm connector
1961ba1b0f6cSNicholas Kazlauskas  * @capable: True if the connector is variable refresh rate capable
1962ba1b0f6cSNicholas Kazlauskas  *
1963ba1b0f6cSNicholas Kazlauskas  * Should be used by atomic drivers to update the indicated support for
1964ba1b0f6cSNicholas Kazlauskas  * variable refresh rate over a connector.
1965ba1b0f6cSNicholas Kazlauskas  */
1966ba1b0f6cSNicholas Kazlauskas void drm_connector_set_vrr_capable_property(
1967ba1b0f6cSNicholas Kazlauskas 		struct drm_connector *connector, bool capable)
1968ba1b0f6cSNicholas Kazlauskas {
1969ba1b0f6cSNicholas Kazlauskas 	drm_object_property_set_value(&connector->base,
1970ba1b0f6cSNicholas Kazlauskas 				      connector->vrr_capable_property,
1971ba1b0f6cSNicholas Kazlauskas 				      capable);
1972ba1b0f6cSNicholas Kazlauskas }
1973ba1b0f6cSNicholas Kazlauskas EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
1974ba1b0f6cSNicholas Kazlauskas 
1975ba1b0f6cSNicholas Kazlauskas /**
19768d70f395SHans de Goede  * drm_connector_init_panel_orientation_property -
19778d70f395SHans de Goede  *	initialize the connecters panel_orientation property
19788d70f395SHans de Goede  * @connector: connector for which to init the panel-orientation property.
19798d70f395SHans de Goede  * @width: width in pixels of the panel, used for panel quirk detection
19808d70f395SHans de Goede  * @height: height in pixels of the panel, used for panel quirk detection
19818d70f395SHans de Goede  *
19828d70f395SHans de Goede  * This function should only be called for built-in panels, after setting
19838d70f395SHans de Goede  * connector->display_info.panel_orientation first (if known).
19848d70f395SHans de Goede  *
19858d70f395SHans de Goede  * This function will check for platform specific (e.g. DMI based) quirks
19868d70f395SHans de Goede  * overriding display_info.panel_orientation first, then if panel_orientation
19878d70f395SHans de Goede  * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
19888d70f395SHans de Goede  * "panel orientation" property to the connector.
19898d70f395SHans de Goede  *
19908d70f395SHans de Goede  * Returns:
19918d70f395SHans de Goede  * Zero on success, negative errno on failure.
19928d70f395SHans de Goede  */
19938d70f395SHans de Goede int drm_connector_init_panel_orientation_property(
19948d70f395SHans de Goede 	struct drm_connector *connector, int width, int height)
19958d70f395SHans de Goede {
19968d70f395SHans de Goede 	struct drm_device *dev = connector->dev;
19978d70f395SHans de Goede 	struct drm_display_info *info = &connector->display_info;
19988d70f395SHans de Goede 	struct drm_property *prop;
19998d70f395SHans de Goede 	int orientation_quirk;
20008d70f395SHans de Goede 
20018d70f395SHans de Goede 	orientation_quirk = drm_get_panel_orientation_quirk(width, height);
20028d70f395SHans de Goede 	if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
20038d70f395SHans de Goede 		info->panel_orientation = orientation_quirk;
20048d70f395SHans de Goede 
20058d70f395SHans de Goede 	if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
20068d70f395SHans de Goede 		return 0;
20078d70f395SHans de Goede 
20088d70f395SHans de Goede 	prop = dev->mode_config.panel_orientation_property;
20098d70f395SHans de Goede 	if (!prop) {
20108d70f395SHans de Goede 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
20118d70f395SHans de Goede 				"panel orientation",
20128d70f395SHans de Goede 				drm_panel_orientation_enum_list,
20138d70f395SHans de Goede 				ARRAY_SIZE(drm_panel_orientation_enum_list));
20148d70f395SHans de Goede 		if (!prop)
20158d70f395SHans de Goede 			return -ENOMEM;
20168d70f395SHans de Goede 
20178d70f395SHans de Goede 		dev->mode_config.panel_orientation_property = prop;
20188d70f395SHans de Goede 	}
20198d70f395SHans de Goede 
20208d70f395SHans de Goede 	drm_object_attach_property(&connector->base, prop,
20218d70f395SHans de Goede 				   info->panel_orientation);
20228d70f395SHans de Goede 	return 0;
20238d70f395SHans de Goede }
20248d70f395SHans de Goede EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
20258d70f395SHans de Goede 
202697e14fbeSDaniel Vetter int drm_connector_set_obj_prop(struct drm_mode_object *obj,
202752217195SDaniel Vetter 				    struct drm_property *property,
202852217195SDaniel Vetter 				    uint64_t value)
202952217195SDaniel Vetter {
203052217195SDaniel Vetter 	int ret = -EINVAL;
203152217195SDaniel Vetter 	struct drm_connector *connector = obj_to_connector(obj);
203252217195SDaniel Vetter 
203352217195SDaniel Vetter 	/* Do DPMS ourselves */
203452217195SDaniel Vetter 	if (property == connector->dev->mode_config.dpms_property) {
203552217195SDaniel Vetter 		ret = (*connector->funcs->dpms)(connector, (int)value);
203652217195SDaniel Vetter 	} else if (connector->funcs->set_property)
203752217195SDaniel Vetter 		ret = connector->funcs->set_property(connector, property, value);
203852217195SDaniel Vetter 
2039144a7999SDaniel Vetter 	if (!ret)
204052217195SDaniel Vetter 		drm_object_property_set_value(&connector->base, property, value);
204152217195SDaniel Vetter 	return ret;
204252217195SDaniel Vetter }
204352217195SDaniel Vetter 
204497e14fbeSDaniel Vetter int drm_connector_property_set_ioctl(struct drm_device *dev,
204552217195SDaniel Vetter 				     void *data, struct drm_file *file_priv)
204652217195SDaniel Vetter {
204752217195SDaniel Vetter 	struct drm_mode_connector_set_property *conn_set_prop = data;
204852217195SDaniel Vetter 	struct drm_mode_obj_set_property obj_set_prop = {
204952217195SDaniel Vetter 		.value = conn_set_prop->value,
205052217195SDaniel Vetter 		.prop_id = conn_set_prop->prop_id,
205152217195SDaniel Vetter 		.obj_id = conn_set_prop->connector_id,
205252217195SDaniel Vetter 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
205352217195SDaniel Vetter 	};
205452217195SDaniel Vetter 
205552217195SDaniel Vetter 	/* It does all the locking and checking we need */
205652217195SDaniel Vetter 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
205752217195SDaniel Vetter }
205852217195SDaniel Vetter 
205952217195SDaniel Vetter static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
206052217195SDaniel Vetter {
206152217195SDaniel Vetter 	/* For atomic drivers only state objects are synchronously updated and
206252217195SDaniel Vetter 	 * protected by modeset locks, so check those first. */
206352217195SDaniel Vetter 	if (connector->state)
206452217195SDaniel Vetter 		return connector->state->best_encoder;
206552217195SDaniel Vetter 	return connector->encoder;
206652217195SDaniel Vetter }
206752217195SDaniel Vetter 
2068c3ff0cdbSAnkit Nautiyal static bool
2069c3ff0cdbSAnkit Nautiyal drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2070c3ff0cdbSAnkit Nautiyal 			     const struct list_head *export_list,
207152217195SDaniel Vetter 			     const struct drm_file *file_priv)
207252217195SDaniel Vetter {
207352217195SDaniel Vetter 	/*
207452217195SDaniel Vetter 	 * If user-space hasn't configured the driver to expose the stereo 3D
207552217195SDaniel Vetter 	 * modes, don't expose them.
207652217195SDaniel Vetter 	 */
207752217195SDaniel Vetter 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
207852217195SDaniel Vetter 		return false;
2079c3ff0cdbSAnkit Nautiyal 	/*
2080c3ff0cdbSAnkit Nautiyal 	 * If user-space hasn't configured the driver to expose the modes
2081c3ff0cdbSAnkit Nautiyal 	 * with aspect-ratio, don't expose them. However if such a mode
2082c3ff0cdbSAnkit Nautiyal 	 * is unique, let it be exposed, but reset the aspect-ratio flags
2083c3ff0cdbSAnkit Nautiyal 	 * while preparing the list of user-modes.
2084c3ff0cdbSAnkit Nautiyal 	 */
2085c3ff0cdbSAnkit Nautiyal 	if (!file_priv->aspect_ratio_allowed) {
2086c3ff0cdbSAnkit Nautiyal 		struct drm_display_mode *mode_itr;
2087c3ff0cdbSAnkit Nautiyal 
2088c3ff0cdbSAnkit Nautiyal 		list_for_each_entry(mode_itr, export_list, export_head)
2089c3ff0cdbSAnkit Nautiyal 			if (drm_mode_match(mode_itr, mode,
2090c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_TIMINGS |
2091c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_CLOCK |
2092c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_FLAGS |
2093c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_3D_FLAGS))
2094c3ff0cdbSAnkit Nautiyal 				return false;
2095c3ff0cdbSAnkit Nautiyal 	}
209652217195SDaniel Vetter 
209752217195SDaniel Vetter 	return true;
209852217195SDaniel Vetter }
209952217195SDaniel Vetter 
210052217195SDaniel Vetter int drm_mode_getconnector(struct drm_device *dev, void *data,
210152217195SDaniel Vetter 			  struct drm_file *file_priv)
210252217195SDaniel Vetter {
210352217195SDaniel Vetter 	struct drm_mode_get_connector *out_resp = data;
210452217195SDaniel Vetter 	struct drm_connector *connector;
210552217195SDaniel Vetter 	struct drm_encoder *encoder;
210652217195SDaniel Vetter 	struct drm_display_mode *mode;
210752217195SDaniel Vetter 	int mode_count = 0;
210852217195SDaniel Vetter 	int encoders_count = 0;
210952217195SDaniel Vetter 	int ret = 0;
211052217195SDaniel Vetter 	int copied = 0;
211152217195SDaniel Vetter 	struct drm_mode_modeinfo u_mode;
211252217195SDaniel Vetter 	struct drm_mode_modeinfo __user *mode_ptr;
211352217195SDaniel Vetter 	uint32_t __user *encoder_ptr;
2114c3ff0cdbSAnkit Nautiyal 	LIST_HEAD(export_list);
211552217195SDaniel Vetter 
211652217195SDaniel Vetter 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
211769fdf420SChris Wilson 		return -EOPNOTSUPP;
211852217195SDaniel Vetter 
211952217195SDaniel Vetter 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
212052217195SDaniel Vetter 
2121418da172SKeith Packard 	connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
212291eefc05SDaniel Vetter 	if (!connector)
212391eefc05SDaniel Vetter 		return -ENOENT;
212452217195SDaniel Vetter 
2125*62afb4adSJosé Roberto de Souza 	encoders_count = hweight32(connector->possible_encoders);
212691eefc05SDaniel Vetter 
212791eefc05SDaniel Vetter 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
212891eefc05SDaniel Vetter 		copied = 0;
212991eefc05SDaniel Vetter 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
213083aefbb8SVille Syrjälä 
2131*62afb4adSJosé Roberto de Souza 		drm_connector_for_each_possible_encoder(connector, encoder) {
213283aefbb8SVille Syrjälä 			if (put_user(encoder->base.id, encoder_ptr + copied)) {
213391eefc05SDaniel Vetter 				ret = -EFAULT;
2134e94ac351SDaniel Vetter 				goto out;
213591eefc05SDaniel Vetter 			}
213691eefc05SDaniel Vetter 			copied++;
213791eefc05SDaniel Vetter 		}
213891eefc05SDaniel Vetter 	}
213991eefc05SDaniel Vetter 	out_resp->count_encoders = encoders_count;
214091eefc05SDaniel Vetter 
214191eefc05SDaniel Vetter 	out_resp->connector_id = connector->base.id;
214291eefc05SDaniel Vetter 	out_resp->connector_type = connector->connector_type;
214391eefc05SDaniel Vetter 	out_resp->connector_type_id = connector->connector_type_id;
214491eefc05SDaniel Vetter 
214591eefc05SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
214691eefc05SDaniel Vetter 	if (out_resp->count_modes == 0) {
214791eefc05SDaniel Vetter 		connector->funcs->fill_modes(connector,
214891eefc05SDaniel Vetter 					     dev->mode_config.max_width,
214991eefc05SDaniel Vetter 					     dev->mode_config.max_height);
215091eefc05SDaniel Vetter 	}
215191eefc05SDaniel Vetter 
215291eefc05SDaniel Vetter 	out_resp->mm_width = connector->display_info.width_mm;
215391eefc05SDaniel Vetter 	out_resp->mm_height = connector->display_info.height_mm;
215491eefc05SDaniel Vetter 	out_resp->subpixel = connector->display_info.subpixel_order;
215591eefc05SDaniel Vetter 	out_resp->connection = connector->status;
215691eefc05SDaniel Vetter 
215791eefc05SDaniel Vetter 	/* delayed so we get modes regardless of pre-fill_modes state */
215891eefc05SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
2159c3ff0cdbSAnkit Nautiyal 		if (drm_mode_expose_to_userspace(mode, &export_list,
2160c3ff0cdbSAnkit Nautiyal 						 file_priv)) {
2161c3ff0cdbSAnkit Nautiyal 			list_add_tail(&mode->export_head, &export_list);
216291eefc05SDaniel Vetter 			mode_count++;
2163c3ff0cdbSAnkit Nautiyal 		}
216491eefc05SDaniel Vetter 
216552217195SDaniel Vetter 	/*
216652217195SDaniel Vetter 	 * This ioctl is called twice, once to determine how much space is
216752217195SDaniel Vetter 	 * needed, and the 2nd time to fill it.
2168c3ff0cdbSAnkit Nautiyal 	 * The modes that need to be exposed to the user are maintained in the
2169c3ff0cdbSAnkit Nautiyal 	 * 'export_list'. When the ioctl is called first time to determine the,
2170c3ff0cdbSAnkit Nautiyal 	 * space, the export_list gets filled, to find the no.of modes. In the
2171c3ff0cdbSAnkit Nautiyal 	 * 2nd time, the user modes are filled, one by one from the export_list.
217252217195SDaniel Vetter 	 */
217352217195SDaniel Vetter 	if ((out_resp->count_modes >= mode_count) && mode_count) {
217452217195SDaniel Vetter 		copied = 0;
217552217195SDaniel Vetter 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2176c3ff0cdbSAnkit Nautiyal 		list_for_each_entry(mode, &export_list, export_head) {
217752217195SDaniel Vetter 			drm_mode_convert_to_umode(&u_mode, mode);
2178c3ff0cdbSAnkit Nautiyal 			/*
2179c3ff0cdbSAnkit Nautiyal 			 * Reset aspect ratio flags of user-mode, if modes with
2180c3ff0cdbSAnkit Nautiyal 			 * aspect-ratio are not supported.
2181c3ff0cdbSAnkit Nautiyal 			 */
2182c3ff0cdbSAnkit Nautiyal 			if (!file_priv->aspect_ratio_allowed)
2183c3ff0cdbSAnkit Nautiyal 				u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
218452217195SDaniel Vetter 			if (copy_to_user(mode_ptr + copied,
218552217195SDaniel Vetter 					 &u_mode, sizeof(u_mode))) {
218652217195SDaniel Vetter 				ret = -EFAULT;
2187e94ac351SDaniel Vetter 				mutex_unlock(&dev->mode_config.mutex);
2188e94ac351SDaniel Vetter 
218952217195SDaniel Vetter 				goto out;
219052217195SDaniel Vetter 			}
219152217195SDaniel Vetter 			copied++;
219252217195SDaniel Vetter 		}
219352217195SDaniel Vetter 	}
219452217195SDaniel Vetter 	out_resp->count_modes = mode_count;
219552217195SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
2196e94ac351SDaniel Vetter 
2197e94ac351SDaniel Vetter 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2198e94ac351SDaniel Vetter 	encoder = drm_connector_get_encoder(connector);
2199e94ac351SDaniel Vetter 	if (encoder)
2200e94ac351SDaniel Vetter 		out_resp->encoder_id = encoder->base.id;
2201e94ac351SDaniel Vetter 	else
2202e94ac351SDaniel Vetter 		out_resp->encoder_id = 0;
2203e94ac351SDaniel Vetter 
2204e94ac351SDaniel Vetter 	/* Only grab properties after probing, to make sure EDID and other
2205e94ac351SDaniel Vetter 	 * properties reflect the latest status. */
2206e94ac351SDaniel Vetter 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
2207e94ac351SDaniel Vetter 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2208e94ac351SDaniel Vetter 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2209e94ac351SDaniel Vetter 			&out_resp->count_props);
2210e94ac351SDaniel Vetter 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2211e94ac351SDaniel Vetter 
2212e94ac351SDaniel Vetter out:
2213ad093607SThierry Reding 	drm_connector_put(connector);
221452217195SDaniel Vetter 
221552217195SDaniel Vetter 	return ret;
221652217195SDaniel Vetter }
221752217195SDaniel Vetter 
22189498c19bSDaniel Vetter 
22199498c19bSDaniel Vetter /**
22209498c19bSDaniel Vetter  * DOC: Tile group
22219498c19bSDaniel Vetter  *
22229498c19bSDaniel Vetter  * Tile groups are used to represent tiled monitors with a unique integer
22239498c19bSDaniel Vetter  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
22249498c19bSDaniel Vetter  * we store this in a tile group, so we have a common identifier for all tiles
22259498c19bSDaniel Vetter  * in a monitor group. The property is called "TILE". Drivers can manage tile
22269498c19bSDaniel Vetter  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
22279498c19bSDaniel Vetter  * drm_mode_get_tile_group(). But this is only needed for internal panels where
22289498c19bSDaniel Vetter  * the tile group information is exposed through a non-standard way.
22299498c19bSDaniel Vetter  */
22309498c19bSDaniel Vetter 
22319498c19bSDaniel Vetter static void drm_tile_group_free(struct kref *kref)
22329498c19bSDaniel Vetter {
22339498c19bSDaniel Vetter 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
22349498c19bSDaniel Vetter 	struct drm_device *dev = tg->dev;
22359498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
22369498c19bSDaniel Vetter 	idr_remove(&dev->mode_config.tile_idr, tg->id);
22379498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
22389498c19bSDaniel Vetter 	kfree(tg);
22399498c19bSDaniel Vetter }
22409498c19bSDaniel Vetter 
22419498c19bSDaniel Vetter /**
22429498c19bSDaniel Vetter  * drm_mode_put_tile_group - drop a reference to a tile group.
22439498c19bSDaniel Vetter  * @dev: DRM device
22449498c19bSDaniel Vetter  * @tg: tile group to drop reference to.
22459498c19bSDaniel Vetter  *
22469498c19bSDaniel Vetter  * drop reference to tile group and free if 0.
22479498c19bSDaniel Vetter  */
22489498c19bSDaniel Vetter void drm_mode_put_tile_group(struct drm_device *dev,
22499498c19bSDaniel Vetter 			     struct drm_tile_group *tg)
22509498c19bSDaniel Vetter {
22519498c19bSDaniel Vetter 	kref_put(&tg->refcount, drm_tile_group_free);
22529498c19bSDaniel Vetter }
22539498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_put_tile_group);
22549498c19bSDaniel Vetter 
22559498c19bSDaniel Vetter /**
22569498c19bSDaniel Vetter  * drm_mode_get_tile_group - get a reference to an existing tile group
22579498c19bSDaniel Vetter  * @dev: DRM device
22589498c19bSDaniel Vetter  * @topology: 8-bytes unique per monitor.
22599498c19bSDaniel Vetter  *
22609498c19bSDaniel Vetter  * Use the unique bytes to get a reference to an existing tile group.
22619498c19bSDaniel Vetter  *
22629498c19bSDaniel Vetter  * RETURNS:
22639498c19bSDaniel Vetter  * tile group or NULL if not found.
22649498c19bSDaniel Vetter  */
22659498c19bSDaniel Vetter struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
22669498c19bSDaniel Vetter 					       char topology[8])
22679498c19bSDaniel Vetter {
22689498c19bSDaniel Vetter 	struct drm_tile_group *tg;
22699498c19bSDaniel Vetter 	int id;
22709498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
22719498c19bSDaniel Vetter 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
22729498c19bSDaniel Vetter 		if (!memcmp(tg->group_data, topology, 8)) {
22739498c19bSDaniel Vetter 			if (!kref_get_unless_zero(&tg->refcount))
22749498c19bSDaniel Vetter 				tg = NULL;
22759498c19bSDaniel Vetter 			mutex_unlock(&dev->mode_config.idr_mutex);
22769498c19bSDaniel Vetter 			return tg;
22779498c19bSDaniel Vetter 		}
22789498c19bSDaniel Vetter 	}
22799498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
22809498c19bSDaniel Vetter 	return NULL;
22819498c19bSDaniel Vetter }
22829498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_get_tile_group);
22839498c19bSDaniel Vetter 
22849498c19bSDaniel Vetter /**
22859498c19bSDaniel Vetter  * drm_mode_create_tile_group - create a tile group from a displayid description
22869498c19bSDaniel Vetter  * @dev: DRM device
22879498c19bSDaniel Vetter  * @topology: 8-bytes unique per monitor.
22889498c19bSDaniel Vetter  *
22899498c19bSDaniel Vetter  * Create a tile group for the unique monitor, and get a unique
22909498c19bSDaniel Vetter  * identifier for the tile group.
22919498c19bSDaniel Vetter  *
22929498c19bSDaniel Vetter  * RETURNS:
2293705c8160SDan Carpenter  * new tile group or NULL.
22949498c19bSDaniel Vetter  */
22959498c19bSDaniel Vetter struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
22969498c19bSDaniel Vetter 						  char topology[8])
22979498c19bSDaniel Vetter {
22989498c19bSDaniel Vetter 	struct drm_tile_group *tg;
22999498c19bSDaniel Vetter 	int ret;
23009498c19bSDaniel Vetter 
23019498c19bSDaniel Vetter 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
23029498c19bSDaniel Vetter 	if (!tg)
2303705c8160SDan Carpenter 		return NULL;
23049498c19bSDaniel Vetter 
23059498c19bSDaniel Vetter 	kref_init(&tg->refcount);
23069498c19bSDaniel Vetter 	memcpy(tg->group_data, topology, 8);
23079498c19bSDaniel Vetter 	tg->dev = dev;
23089498c19bSDaniel Vetter 
23099498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
23109498c19bSDaniel Vetter 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
23119498c19bSDaniel Vetter 	if (ret >= 0) {
23129498c19bSDaniel Vetter 		tg->id = ret;
23139498c19bSDaniel Vetter 	} else {
23149498c19bSDaniel Vetter 		kfree(tg);
2315705c8160SDan Carpenter 		tg = NULL;
23169498c19bSDaniel Vetter 	}
23179498c19bSDaniel Vetter 
23189498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
23199498c19bSDaniel Vetter 	return tg;
23209498c19bSDaniel Vetter }
23219498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_create_tile_group);
2322