xref: /linux/drivers/gpu/drm/drm_connector.c (revision 39b50c603878f4f8ae541ac4088a805d588abc79)
152217195SDaniel Vetter /*
252217195SDaniel Vetter  * Copyright (c) 2016 Intel Corporation
352217195SDaniel Vetter  *
452217195SDaniel Vetter  * Permission to use, copy, modify, distribute, and sell this software and its
552217195SDaniel Vetter  * documentation for any purpose is hereby granted without fee, provided that
652217195SDaniel Vetter  * the above copyright notice appear in all copies and that both that copyright
752217195SDaniel Vetter  * notice and this permission notice appear in supporting documentation, and
852217195SDaniel Vetter  * that the name of the copyright holders not be used in advertising or
952217195SDaniel Vetter  * publicity pertaining to distribution of the software without specific,
1052217195SDaniel Vetter  * written prior permission.  The copyright holders make no representations
1152217195SDaniel Vetter  * about the suitability of this software for any purpose.  It is provided "as
1252217195SDaniel Vetter  * is" without express or implied warranty.
1352217195SDaniel Vetter  *
1452217195SDaniel Vetter  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1552217195SDaniel Vetter  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1652217195SDaniel Vetter  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1752217195SDaniel Vetter  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1852217195SDaniel Vetter  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1952217195SDaniel Vetter  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2052217195SDaniel Vetter  * OF THIS SOFTWARE.
2152217195SDaniel Vetter  */
2252217195SDaniel Vetter 
2352217195SDaniel Vetter #include <drm/drm_connector.h>
2452217195SDaniel Vetter #include <drm/drm_edid.h>
259338203cSLaurent Pinchart #include <drm/drm_encoder.h>
268d70f395SHans de Goede #include <drm/drm_utils.h>
2799f45e32SDaniel Vetter #include <drm/drm_print.h>
2899f45e32SDaniel Vetter #include <drm/drm_drv.h>
2999f45e32SDaniel Vetter #include <drm/drm_file.h>
3099f45e32SDaniel Vetter 
3199f45e32SDaniel Vetter #include <linux/uaccess.h>
3252217195SDaniel Vetter 
3352217195SDaniel Vetter #include "drm_crtc_internal.h"
3452217195SDaniel Vetter #include "drm_internal.h"
3552217195SDaniel Vetter 
36ae2a6da8SDaniel Vetter /**
37ae2a6da8SDaniel Vetter  * DOC: overview
38ae2a6da8SDaniel Vetter  *
39ae2a6da8SDaniel Vetter  * In DRM connectors are the general abstraction for display sinks, and include
40ae2a6da8SDaniel Vetter  * als fixed panels or anything else that can display pixels in some form. As
41ae2a6da8SDaniel Vetter  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
42ae2a6da8SDaniel Vetter  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
43ad093607SThierry Reding  * Hence they are reference-counted using drm_connector_get() and
44ad093607SThierry Reding  * drm_connector_put().
45ae2a6da8SDaniel Vetter  *
46d574528aSDaniel Vetter  * KMS driver must create, initialize, register and attach at a &struct
47d574528aSDaniel Vetter  * drm_connector for each such sink. The instance is created as other KMS
48aec97460SDaniel Vetter  * objects and initialized by setting the following fields. The connector is
49aec97460SDaniel Vetter  * initialized with a call to drm_connector_init() with a pointer to the
50aec97460SDaniel Vetter  * &struct drm_connector_funcs and a connector type, and then exposed to
51aec97460SDaniel Vetter  * userspace with a call to drm_connector_register().
52ae2a6da8SDaniel Vetter  *
53ae2a6da8SDaniel Vetter  * Connectors must be attached to an encoder to be used. For devices that map
54ae2a6da8SDaniel Vetter  * connectors to encoders 1:1, the connector should be attached at
55cde4c44dSDaniel Vetter  * initialization time with a call to drm_connector_attach_encoder(). The
56d574528aSDaniel Vetter  * driver must also set the &drm_connector.encoder field to point to the
57ae2a6da8SDaniel Vetter  * attached encoder.
58ae2a6da8SDaniel Vetter  *
59ae2a6da8SDaniel Vetter  * For connectors which are not fixed (like built-in panels) the driver needs to
60ae2a6da8SDaniel Vetter  * support hotplug notifications. The simplest way to do that is by using the
61ae2a6da8SDaniel Vetter  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
62ae2a6da8SDaniel Vetter  * hardware support for hotplug interrupts. Connectors with hardware hotplug
63ae2a6da8SDaniel Vetter  * support can instead use e.g. drm_helper_hpd_irq_event().
64ae2a6da8SDaniel Vetter  */
65ae2a6da8SDaniel Vetter 
6652217195SDaniel Vetter struct drm_conn_prop_enum_list {
6752217195SDaniel Vetter 	int type;
6852217195SDaniel Vetter 	const char *name;
6952217195SDaniel Vetter 	struct ida ida;
7052217195SDaniel Vetter };
7152217195SDaniel Vetter 
7252217195SDaniel Vetter /*
7352217195SDaniel Vetter  * Connector and encoder types.
7452217195SDaniel Vetter  */
7552217195SDaniel Vetter static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
7652217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
7752217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
7852217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
7952217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
8052217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
8152217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
8252217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
8352217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
8452217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_Component, "Component" },
8552217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
8652217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
8752217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
8852217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
8952217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_TV, "TV" },
9052217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
9152217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
9252217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
9352217195SDaniel Vetter 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
94935774cdSBrian Starkey 	{ DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
9552217195SDaniel Vetter };
9652217195SDaniel Vetter 
9752217195SDaniel Vetter void drm_connector_ida_init(void)
9852217195SDaniel Vetter {
9952217195SDaniel Vetter 	int i;
10052217195SDaniel Vetter 
10152217195SDaniel Vetter 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
10252217195SDaniel Vetter 		ida_init(&drm_connector_enum_list[i].ida);
10352217195SDaniel Vetter }
10452217195SDaniel Vetter 
10552217195SDaniel Vetter void drm_connector_ida_destroy(void)
10652217195SDaniel Vetter {
10752217195SDaniel Vetter 	int i;
10852217195SDaniel Vetter 
10952217195SDaniel Vetter 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
11052217195SDaniel Vetter 		ida_destroy(&drm_connector_enum_list[i].ida);
11152217195SDaniel Vetter }
11252217195SDaniel Vetter 
11352217195SDaniel Vetter /**
11452217195SDaniel Vetter  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
11552217195SDaniel Vetter  * @connector: connector to quwery
11652217195SDaniel Vetter  *
117ae2a6da8SDaniel Vetter  * The kernel supports per-connector configuration of its consoles through
11852217195SDaniel Vetter  * use of the video= parameter. This function parses that option and
11952217195SDaniel Vetter  * extracts the user's specified mode (or enable/disable status) for a
12052217195SDaniel Vetter  * particular connector. This is typically only used during the early fbdev
12152217195SDaniel Vetter  * setup.
12252217195SDaniel Vetter  */
12352217195SDaniel Vetter static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
12452217195SDaniel Vetter {
12552217195SDaniel Vetter 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
12652217195SDaniel Vetter 	char *option = NULL;
12752217195SDaniel Vetter 
12852217195SDaniel Vetter 	if (fb_get_options(connector->name, &option))
12952217195SDaniel Vetter 		return;
13052217195SDaniel Vetter 
13152217195SDaniel Vetter 	if (!drm_mode_parse_command_line_for_connector(option,
13252217195SDaniel Vetter 						       connector,
13352217195SDaniel Vetter 						       mode))
13452217195SDaniel Vetter 		return;
13552217195SDaniel Vetter 
13652217195SDaniel Vetter 	if (mode->force) {
1376140cf20SJani Nikula 		DRM_INFO("forcing %s connector %s\n", connector->name,
1386140cf20SJani Nikula 			 drm_get_connector_force_name(mode->force));
13952217195SDaniel Vetter 		connector->force = mode->force;
14052217195SDaniel Vetter 	}
14152217195SDaniel Vetter 
14252217195SDaniel Vetter 	DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
14352217195SDaniel Vetter 		      connector->name,
14452217195SDaniel Vetter 		      mode->xres, mode->yres,
14552217195SDaniel Vetter 		      mode->refresh_specified ? mode->refresh : 60,
14652217195SDaniel Vetter 		      mode->rb ? " reduced blanking" : "",
14752217195SDaniel Vetter 		      mode->margins ? " with margins" : "",
14852217195SDaniel Vetter 		      mode->interlace ?  " interlaced" : "");
14952217195SDaniel Vetter }
15052217195SDaniel Vetter 
15152217195SDaniel Vetter static void drm_connector_free(struct kref *kref)
15252217195SDaniel Vetter {
15352217195SDaniel Vetter 	struct drm_connector *connector =
15452217195SDaniel Vetter 		container_of(kref, struct drm_connector, base.refcount);
15552217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
15652217195SDaniel Vetter 
15752217195SDaniel Vetter 	drm_mode_object_unregister(dev, &connector->base);
15852217195SDaniel Vetter 	connector->funcs->destroy(connector);
15952217195SDaniel Vetter }
16052217195SDaniel Vetter 
161ea497bb9SDaniel Vetter void drm_connector_free_work_fn(struct work_struct *work)
162a703c550SDaniel Vetter {
163ea497bb9SDaniel Vetter 	struct drm_connector *connector, *n;
164ea497bb9SDaniel Vetter 	struct drm_device *dev =
165ea497bb9SDaniel Vetter 		container_of(work, struct drm_device, mode_config.connector_free_work);
166ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &dev->mode_config;
167ea497bb9SDaniel Vetter 	unsigned long flags;
168ea497bb9SDaniel Vetter 	struct llist_node *freed;
169a703c550SDaniel Vetter 
170ea497bb9SDaniel Vetter 	spin_lock_irqsave(&config->connector_list_lock, flags);
171ea497bb9SDaniel Vetter 	freed = llist_del_all(&config->connector_free_list);
172ea497bb9SDaniel Vetter 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
173ea497bb9SDaniel Vetter 
174ea497bb9SDaniel Vetter 	llist_for_each_entry_safe(connector, n, freed, free_node) {
175a703c550SDaniel Vetter 		drm_mode_object_unregister(dev, &connector->base);
176a703c550SDaniel Vetter 		connector->funcs->destroy(connector);
177a703c550SDaniel Vetter 	}
178ea497bb9SDaniel Vetter }
179a703c550SDaniel Vetter 
18052217195SDaniel Vetter /**
18152217195SDaniel Vetter  * drm_connector_init - Init a preallocated connector
18252217195SDaniel Vetter  * @dev: DRM device
18352217195SDaniel Vetter  * @connector: the connector to init
18452217195SDaniel Vetter  * @funcs: callbacks for this connector
18552217195SDaniel Vetter  * @connector_type: user visible type of the connector
18652217195SDaniel Vetter  *
18752217195SDaniel Vetter  * Initialises a preallocated connector. Connectors should be
18852217195SDaniel Vetter  * subclassed as part of driver connector objects.
18952217195SDaniel Vetter  *
19052217195SDaniel Vetter  * Returns:
19152217195SDaniel Vetter  * Zero on success, error code on failure.
19252217195SDaniel Vetter  */
19352217195SDaniel Vetter int drm_connector_init(struct drm_device *dev,
19452217195SDaniel Vetter 		       struct drm_connector *connector,
19552217195SDaniel Vetter 		       const struct drm_connector_funcs *funcs,
19652217195SDaniel Vetter 		       int connector_type)
19752217195SDaniel Vetter {
19852217195SDaniel Vetter 	struct drm_mode_config *config = &dev->mode_config;
19952217195SDaniel Vetter 	int ret;
20052217195SDaniel Vetter 	struct ida *connector_ida =
20152217195SDaniel Vetter 		&drm_connector_enum_list[connector_type].ida;
20252217195SDaniel Vetter 
203ba1f665fSHaneen Mohammed 	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
204ba1f665fSHaneen Mohammed 		(!funcs->atomic_destroy_state ||
205ba1f665fSHaneen Mohammed 		 !funcs->atomic_duplicate_state));
206ba1f665fSHaneen Mohammed 
2072135ea7aSThierry Reding 	ret = __drm_mode_object_add(dev, &connector->base,
20852217195SDaniel Vetter 				    DRM_MODE_OBJECT_CONNECTOR,
20952217195SDaniel Vetter 				    false, drm_connector_free);
21052217195SDaniel Vetter 	if (ret)
211613051daSDaniel Vetter 		return ret;
21252217195SDaniel Vetter 
21352217195SDaniel Vetter 	connector->base.properties = &connector->properties;
21452217195SDaniel Vetter 	connector->dev = dev;
21552217195SDaniel Vetter 	connector->funcs = funcs;
21652217195SDaniel Vetter 
2172a8d3eacSVille Syrjälä 	/* connector index is used with 32bit bitmasks */
2182a8d3eacSVille Syrjälä 	ret = ida_simple_get(&config->connector_ida, 0, 32, GFP_KERNEL);
2192a8d3eacSVille Syrjälä 	if (ret < 0) {
2202a8d3eacSVille Syrjälä 		DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
2212a8d3eacSVille Syrjälä 			      drm_connector_enum_list[connector_type].name,
2222a8d3eacSVille Syrjälä 			      ret);
22352217195SDaniel Vetter 		goto out_put;
2242a8d3eacSVille Syrjälä 	}
22552217195SDaniel Vetter 	connector->index = ret;
22652217195SDaniel Vetter 	ret = 0;
22752217195SDaniel Vetter 
22852217195SDaniel Vetter 	connector->connector_type = connector_type;
22952217195SDaniel Vetter 	connector->connector_type_id =
23052217195SDaniel Vetter 		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
23152217195SDaniel Vetter 	if (connector->connector_type_id < 0) {
23252217195SDaniel Vetter 		ret = connector->connector_type_id;
23352217195SDaniel Vetter 		goto out_put_id;
23452217195SDaniel Vetter 	}
23552217195SDaniel Vetter 	connector->name =
23652217195SDaniel Vetter 		kasprintf(GFP_KERNEL, "%s-%d",
23752217195SDaniel Vetter 			  drm_connector_enum_list[connector_type].name,
23852217195SDaniel Vetter 			  connector->connector_type_id);
23952217195SDaniel Vetter 	if (!connector->name) {
24052217195SDaniel Vetter 		ret = -ENOMEM;
24152217195SDaniel Vetter 		goto out_put_type_id;
24252217195SDaniel Vetter 	}
24352217195SDaniel Vetter 
24452217195SDaniel Vetter 	INIT_LIST_HEAD(&connector->probed_modes);
24552217195SDaniel Vetter 	INIT_LIST_HEAD(&connector->modes);
246e73ab00eSDaniel Vetter 	mutex_init(&connector->mutex);
24752217195SDaniel Vetter 	connector->edid_blob_ptr = NULL;
24852217195SDaniel Vetter 	connector->status = connector_status_unknown;
2498d70f395SHans de Goede 	connector->display_info.panel_orientation =
2508d70f395SHans de Goede 		DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
25152217195SDaniel Vetter 
25252217195SDaniel Vetter 	drm_connector_get_cmdline_mode(connector);
25352217195SDaniel Vetter 
25452217195SDaniel Vetter 	/* We should add connectors at the end to avoid upsetting the connector
25552217195SDaniel Vetter 	 * index too much. */
256613051daSDaniel Vetter 	spin_lock_irq(&config->connector_list_lock);
25752217195SDaniel Vetter 	list_add_tail(&connector->head, &config->connector_list);
25852217195SDaniel Vetter 	config->num_connector++;
259613051daSDaniel Vetter 	spin_unlock_irq(&config->connector_list_lock);
26052217195SDaniel Vetter 
261935774cdSBrian Starkey 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
262935774cdSBrian Starkey 	    connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
26352217195SDaniel Vetter 		drm_object_attach_property(&connector->base,
26452217195SDaniel Vetter 					      config->edid_property,
26552217195SDaniel Vetter 					      0);
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);
27766660d4cSDave Airlie 
27852217195SDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
27952217195SDaniel Vetter 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
28052217195SDaniel Vetter 	}
28152217195SDaniel Vetter 
28252217195SDaniel Vetter 	connector->debugfs_entry = NULL;
28352217195SDaniel Vetter out_put_type_id:
28452217195SDaniel Vetter 	if (ret)
285587680c1SChristophe JAILLET 		ida_simple_remove(connector_ida, connector->connector_type_id);
28652217195SDaniel Vetter out_put_id:
28752217195SDaniel Vetter 	if (ret)
288587680c1SChristophe JAILLET 		ida_simple_remove(&config->connector_ida, connector->index);
28952217195SDaniel Vetter out_put:
29052217195SDaniel Vetter 	if (ret)
29152217195SDaniel Vetter 		drm_mode_object_unregister(dev, &connector->base);
29252217195SDaniel Vetter 
29352217195SDaniel Vetter 	return ret;
29452217195SDaniel Vetter }
29552217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_init);
29652217195SDaniel Vetter 
29752217195SDaniel Vetter /**
298cde4c44dSDaniel Vetter  * drm_connector_attach_encoder - attach a connector to an encoder
29952217195SDaniel Vetter  * @connector: connector to attach
30052217195SDaniel Vetter  * @encoder: encoder to attach @connector to
30152217195SDaniel Vetter  *
30252217195SDaniel Vetter  * This function links up a connector to an encoder. Note that the routing
30352217195SDaniel Vetter  * restrictions between encoders and crtcs are exposed to userspace through the
30452217195SDaniel Vetter  * possible_clones and possible_crtcs bitmasks.
30552217195SDaniel Vetter  *
30652217195SDaniel Vetter  * Returns:
30752217195SDaniel Vetter  * Zero on success, negative errno on failure.
30852217195SDaniel Vetter  */
309cde4c44dSDaniel Vetter int drm_connector_attach_encoder(struct drm_connector *connector,
31052217195SDaniel Vetter 				 struct drm_encoder *encoder)
31152217195SDaniel Vetter {
31252217195SDaniel Vetter 	int i;
31352217195SDaniel Vetter 
31452217195SDaniel Vetter 	/*
31552217195SDaniel Vetter 	 * In the past, drivers have attempted to model the static association
31652217195SDaniel Vetter 	 * of connector to encoder in simple connector/encoder devices using a
31752217195SDaniel Vetter 	 * direct assignment of connector->encoder = encoder. This connection
31852217195SDaniel Vetter 	 * is a logical one and the responsibility of the core, so drivers are
31952217195SDaniel Vetter 	 * expected not to mess with this.
32052217195SDaniel Vetter 	 *
32152217195SDaniel Vetter 	 * Note that the error return should've been enough here, but a large
32252217195SDaniel Vetter 	 * majority of drivers ignores the return value, so add in a big WARN
32352217195SDaniel Vetter 	 * to get people's attention.
32452217195SDaniel Vetter 	 */
32552217195SDaniel Vetter 	if (WARN_ON(connector->encoder))
32652217195SDaniel Vetter 		return -EINVAL;
32752217195SDaniel Vetter 
32883aefbb8SVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(connector->encoder_ids); i++) {
32952217195SDaniel Vetter 		if (connector->encoder_ids[i] == 0) {
33052217195SDaniel Vetter 			connector->encoder_ids[i] = encoder->base.id;
33152217195SDaniel Vetter 			return 0;
33252217195SDaniel Vetter 		}
33352217195SDaniel Vetter 	}
33452217195SDaniel Vetter 	return -ENOMEM;
33552217195SDaniel Vetter }
336cde4c44dSDaniel Vetter EXPORT_SYMBOL(drm_connector_attach_encoder);
33752217195SDaniel Vetter 
33838cb8d96SVille Syrjälä /**
33938cb8d96SVille Syrjälä  * drm_connector_has_possible_encoder - check if the connector and encoder are assosicated with each other
34038cb8d96SVille Syrjälä  * @connector: the connector
34138cb8d96SVille Syrjälä  * @encoder: the encoder
34238cb8d96SVille Syrjälä  *
34338cb8d96SVille Syrjälä  * Returns:
34438cb8d96SVille Syrjälä  * True if @encoder is one of the possible encoders for @connector.
34538cb8d96SVille Syrjälä  */
34638cb8d96SVille Syrjälä bool drm_connector_has_possible_encoder(struct drm_connector *connector,
34738cb8d96SVille Syrjälä 					struct drm_encoder *encoder)
34838cb8d96SVille Syrjälä {
34938cb8d96SVille Syrjälä 	struct drm_encoder *enc;
35038cb8d96SVille Syrjälä 	int i;
35138cb8d96SVille Syrjälä 
35238cb8d96SVille Syrjälä 	drm_connector_for_each_possible_encoder(connector, enc, i) {
35338cb8d96SVille Syrjälä 		if (enc == encoder)
35438cb8d96SVille Syrjälä 			return true;
35538cb8d96SVille Syrjälä 	}
35638cb8d96SVille Syrjälä 
35738cb8d96SVille Syrjälä 	return false;
35838cb8d96SVille Syrjälä }
35938cb8d96SVille Syrjälä EXPORT_SYMBOL(drm_connector_has_possible_encoder);
36038cb8d96SVille Syrjälä 
36152217195SDaniel Vetter static void drm_mode_remove(struct drm_connector *connector,
36252217195SDaniel Vetter 			    struct drm_display_mode *mode)
36352217195SDaniel Vetter {
36452217195SDaniel Vetter 	list_del(&mode->head);
36552217195SDaniel Vetter 	drm_mode_destroy(connector->dev, mode);
36652217195SDaniel Vetter }
36752217195SDaniel Vetter 
36852217195SDaniel Vetter /**
36952217195SDaniel Vetter  * drm_connector_cleanup - cleans up an initialised connector
37052217195SDaniel Vetter  * @connector: connector to cleanup
37152217195SDaniel Vetter  *
37252217195SDaniel Vetter  * Cleans up the connector but doesn't free the object.
37352217195SDaniel Vetter  */
37452217195SDaniel Vetter void drm_connector_cleanup(struct drm_connector *connector)
37552217195SDaniel Vetter {
37652217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
37752217195SDaniel Vetter 	struct drm_display_mode *mode, *t;
37852217195SDaniel Vetter 
37952217195SDaniel Vetter 	/* The connector should have been removed from userspace long before
38052217195SDaniel Vetter 	 * it is finally destroyed.
38152217195SDaniel Vetter 	 */
382*39b50c60SLyude Paul 	if (WARN_ON(connector->registration_state ==
383*39b50c60SLyude Paul 		    DRM_CONNECTOR_REGISTERED))
38452217195SDaniel Vetter 		drm_connector_unregister(connector);
38552217195SDaniel Vetter 
38652217195SDaniel Vetter 	if (connector->tile_group) {
38752217195SDaniel Vetter 		drm_mode_put_tile_group(dev, connector->tile_group);
38852217195SDaniel Vetter 		connector->tile_group = NULL;
38952217195SDaniel Vetter 	}
39052217195SDaniel Vetter 
39152217195SDaniel Vetter 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
39252217195SDaniel Vetter 		drm_mode_remove(connector, mode);
39352217195SDaniel Vetter 
39452217195SDaniel Vetter 	list_for_each_entry_safe(mode, t, &connector->modes, head)
39552217195SDaniel Vetter 		drm_mode_remove(connector, mode);
39652217195SDaniel Vetter 
3979a47dba1SChristophe JAILLET 	ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
39852217195SDaniel Vetter 			  connector->connector_type_id);
39952217195SDaniel Vetter 
4009a47dba1SChristophe JAILLET 	ida_simple_remove(&dev->mode_config.connector_ida,
40152217195SDaniel Vetter 			  connector->index);
40252217195SDaniel Vetter 
40352217195SDaniel Vetter 	kfree(connector->display_info.bus_formats);
40452217195SDaniel Vetter 	drm_mode_object_unregister(dev, &connector->base);
40552217195SDaniel Vetter 	kfree(connector->name);
40652217195SDaniel Vetter 	connector->name = NULL;
407613051daSDaniel Vetter 	spin_lock_irq(&dev->mode_config.connector_list_lock);
40852217195SDaniel Vetter 	list_del(&connector->head);
40952217195SDaniel Vetter 	dev->mode_config.num_connector--;
410613051daSDaniel Vetter 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
41152217195SDaniel Vetter 
41252217195SDaniel Vetter 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
41352217195SDaniel Vetter 	if (connector->state && connector->funcs->atomic_destroy_state)
41452217195SDaniel Vetter 		connector->funcs->atomic_destroy_state(connector,
41552217195SDaniel Vetter 						       connector->state);
41652217195SDaniel Vetter 
417e73ab00eSDaniel Vetter 	mutex_destroy(&connector->mutex);
418e73ab00eSDaniel Vetter 
41952217195SDaniel Vetter 	memset(connector, 0, sizeof(*connector));
42052217195SDaniel Vetter }
42152217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_cleanup);
42252217195SDaniel Vetter 
42352217195SDaniel Vetter /**
42452217195SDaniel Vetter  * drm_connector_register - register a connector
42552217195SDaniel Vetter  * @connector: the connector to register
42652217195SDaniel Vetter  *
42752217195SDaniel Vetter  * Register userspace interfaces for a connector
42852217195SDaniel Vetter  *
42952217195SDaniel Vetter  * Returns:
43052217195SDaniel Vetter  * Zero on success, error code on failure.
43152217195SDaniel Vetter  */
43252217195SDaniel Vetter int drm_connector_register(struct drm_connector *connector)
43352217195SDaniel Vetter {
434e73ab00eSDaniel Vetter 	int ret = 0;
43552217195SDaniel Vetter 
436e6e7b48bSDaniel Vetter 	if (!connector->dev->registered)
437e6e7b48bSDaniel Vetter 		return 0;
438e6e7b48bSDaniel Vetter 
439e73ab00eSDaniel Vetter 	mutex_lock(&connector->mutex);
440*39b50c60SLyude Paul 	if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
441e73ab00eSDaniel Vetter 		goto unlock;
44252217195SDaniel Vetter 
44352217195SDaniel Vetter 	ret = drm_sysfs_connector_add(connector);
44452217195SDaniel Vetter 	if (ret)
445e73ab00eSDaniel Vetter 		goto unlock;
44652217195SDaniel Vetter 
44752217195SDaniel Vetter 	ret = drm_debugfs_connector_add(connector);
44852217195SDaniel Vetter 	if (ret) {
44952217195SDaniel Vetter 		goto err_sysfs;
45052217195SDaniel Vetter 	}
45152217195SDaniel Vetter 
45252217195SDaniel Vetter 	if (connector->funcs->late_register) {
45352217195SDaniel Vetter 		ret = connector->funcs->late_register(connector);
45452217195SDaniel Vetter 		if (ret)
45552217195SDaniel Vetter 			goto err_debugfs;
45652217195SDaniel Vetter 	}
45752217195SDaniel Vetter 
45852217195SDaniel Vetter 	drm_mode_object_register(connector->dev, &connector->base);
45952217195SDaniel Vetter 
460*39b50c60SLyude Paul 	connector->registration_state = DRM_CONNECTOR_REGISTERED;
461e73ab00eSDaniel Vetter 	goto unlock;
46252217195SDaniel Vetter 
46352217195SDaniel Vetter err_debugfs:
46452217195SDaniel Vetter 	drm_debugfs_connector_remove(connector);
46552217195SDaniel Vetter err_sysfs:
46652217195SDaniel Vetter 	drm_sysfs_connector_remove(connector);
467e73ab00eSDaniel Vetter unlock:
468e73ab00eSDaniel Vetter 	mutex_unlock(&connector->mutex);
46952217195SDaniel Vetter 	return ret;
47052217195SDaniel Vetter }
47152217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_register);
47252217195SDaniel Vetter 
47352217195SDaniel Vetter /**
47452217195SDaniel Vetter  * drm_connector_unregister - unregister a connector
47552217195SDaniel Vetter  * @connector: the connector to unregister
47652217195SDaniel Vetter  *
47752217195SDaniel Vetter  * Unregister userspace interfaces for a connector
47852217195SDaniel Vetter  */
47952217195SDaniel Vetter void drm_connector_unregister(struct drm_connector *connector)
48052217195SDaniel Vetter {
481e73ab00eSDaniel Vetter 	mutex_lock(&connector->mutex);
482*39b50c60SLyude Paul 	if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
483e73ab00eSDaniel Vetter 		mutex_unlock(&connector->mutex);
48452217195SDaniel Vetter 		return;
485e73ab00eSDaniel Vetter 	}
48652217195SDaniel Vetter 
48752217195SDaniel Vetter 	if (connector->funcs->early_unregister)
48852217195SDaniel Vetter 		connector->funcs->early_unregister(connector);
48952217195SDaniel Vetter 
49052217195SDaniel Vetter 	drm_sysfs_connector_remove(connector);
49152217195SDaniel Vetter 	drm_debugfs_connector_remove(connector);
49252217195SDaniel Vetter 
493*39b50c60SLyude Paul 	connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
494e73ab00eSDaniel Vetter 	mutex_unlock(&connector->mutex);
49552217195SDaniel Vetter }
49652217195SDaniel Vetter EXPORT_SYMBOL(drm_connector_unregister);
49752217195SDaniel Vetter 
49852217195SDaniel Vetter void drm_connector_unregister_all(struct drm_device *dev)
49952217195SDaniel Vetter {
50052217195SDaniel Vetter 	struct drm_connector *connector;
501613051daSDaniel Vetter 	struct drm_connector_list_iter conn_iter;
50252217195SDaniel Vetter 
503b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
504613051daSDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter)
50552217195SDaniel Vetter 		drm_connector_unregister(connector);
506b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
50752217195SDaniel Vetter }
50852217195SDaniel Vetter 
50952217195SDaniel Vetter int drm_connector_register_all(struct drm_device *dev)
51052217195SDaniel Vetter {
51152217195SDaniel Vetter 	struct drm_connector *connector;
512613051daSDaniel Vetter 	struct drm_connector_list_iter conn_iter;
513613051daSDaniel Vetter 	int ret = 0;
51452217195SDaniel Vetter 
515b982dab1SThierry Reding 	drm_connector_list_iter_begin(dev, &conn_iter);
516613051daSDaniel Vetter 	drm_for_each_connector_iter(connector, &conn_iter) {
51752217195SDaniel Vetter 		ret = drm_connector_register(connector);
51852217195SDaniel Vetter 		if (ret)
519613051daSDaniel Vetter 			break;
52052217195SDaniel Vetter 	}
521b982dab1SThierry Reding 	drm_connector_list_iter_end(&conn_iter);
52252217195SDaniel Vetter 
523613051daSDaniel Vetter 	if (ret)
52452217195SDaniel Vetter 		drm_connector_unregister_all(dev);
52552217195SDaniel Vetter 	return ret;
52652217195SDaniel Vetter }
52752217195SDaniel Vetter 
52852217195SDaniel Vetter /**
52952217195SDaniel Vetter  * drm_get_connector_status_name - return a string for connector status
53052217195SDaniel Vetter  * @status: connector status to compute name of
53152217195SDaniel Vetter  *
53252217195SDaniel Vetter  * In contrast to the other drm_get_*_name functions this one here returns a
53352217195SDaniel Vetter  * const pointer and hence is threadsafe.
53452217195SDaniel Vetter  */
53552217195SDaniel Vetter const char *drm_get_connector_status_name(enum drm_connector_status status)
53652217195SDaniel Vetter {
53752217195SDaniel Vetter 	if (status == connector_status_connected)
53852217195SDaniel Vetter 		return "connected";
53952217195SDaniel Vetter 	else if (status == connector_status_disconnected)
54052217195SDaniel Vetter 		return "disconnected";
54152217195SDaniel Vetter 	else
54252217195SDaniel Vetter 		return "unknown";
54352217195SDaniel Vetter }
54452217195SDaniel Vetter EXPORT_SYMBOL(drm_get_connector_status_name);
54552217195SDaniel Vetter 
5466140cf20SJani Nikula /**
5476140cf20SJani Nikula  * drm_get_connector_force_name - return a string for connector force
5486140cf20SJani Nikula  * @force: connector force to get name of
5496140cf20SJani Nikula  *
5506140cf20SJani Nikula  * Returns: const pointer to name.
5516140cf20SJani Nikula  */
5526140cf20SJani Nikula const char *drm_get_connector_force_name(enum drm_connector_force force)
5536140cf20SJani Nikula {
5546140cf20SJani Nikula 	switch (force) {
5556140cf20SJani Nikula 	case DRM_FORCE_UNSPECIFIED:
5566140cf20SJani Nikula 		return "unspecified";
5576140cf20SJani Nikula 	case DRM_FORCE_OFF:
5586140cf20SJani Nikula 		return "off";
5596140cf20SJani Nikula 	case DRM_FORCE_ON:
5606140cf20SJani Nikula 		return "on";
5616140cf20SJani Nikula 	case DRM_FORCE_ON_DIGITAL:
5626140cf20SJani Nikula 		return "digital";
5636140cf20SJani Nikula 	default:
5646140cf20SJani Nikula 		return "unknown";
5656140cf20SJani Nikula 	}
5666140cf20SJani Nikula }
5676140cf20SJani Nikula 
568613051daSDaniel Vetter #ifdef CONFIG_LOCKDEP
569613051daSDaniel Vetter static struct lockdep_map connector_list_iter_dep_map = {
570613051daSDaniel Vetter 	.name = "drm_connector_list_iter"
571613051daSDaniel Vetter };
572613051daSDaniel Vetter #endif
573613051daSDaniel Vetter 
574613051daSDaniel Vetter /**
575b982dab1SThierry Reding  * drm_connector_list_iter_begin - initialize a connector_list iterator
576613051daSDaniel Vetter  * @dev: DRM device
577613051daSDaniel Vetter  * @iter: connector_list iterator
578613051daSDaniel Vetter  *
579d574528aSDaniel Vetter  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
580b982dab1SThierry Reding  * must always be cleaned up again by calling drm_connector_list_iter_end().
581613051daSDaniel Vetter  * Iteration itself happens using drm_connector_list_iter_next() or
582613051daSDaniel Vetter  * drm_for_each_connector_iter().
583613051daSDaniel Vetter  */
584b982dab1SThierry Reding void drm_connector_list_iter_begin(struct drm_device *dev,
585613051daSDaniel Vetter 				   struct drm_connector_list_iter *iter)
586613051daSDaniel Vetter {
587613051daSDaniel Vetter 	iter->dev = dev;
588613051daSDaniel Vetter 	iter->conn = NULL;
589613051daSDaniel Vetter 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
590613051daSDaniel Vetter }
591b982dab1SThierry Reding EXPORT_SYMBOL(drm_connector_list_iter_begin);
592613051daSDaniel Vetter 
593a703c550SDaniel Vetter /*
594a703c550SDaniel Vetter  * Extra-safe connector put function that works in any context. Should only be
595a703c550SDaniel Vetter  * used from the connector_iter functions, where we never really expect to
596a703c550SDaniel Vetter  * actually release the connector when dropping our final reference.
597a703c550SDaniel Vetter  */
598a703c550SDaniel Vetter static void
599ea497bb9SDaniel Vetter __drm_connector_put_safe(struct drm_connector *conn)
600a703c550SDaniel Vetter {
601ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &conn->dev->mode_config;
602ea497bb9SDaniel Vetter 
603ea497bb9SDaniel Vetter 	lockdep_assert_held(&config->connector_list_lock);
604ea497bb9SDaniel Vetter 
605ea497bb9SDaniel Vetter 	if (!refcount_dec_and_test(&conn->base.refcount.refcount))
606ea497bb9SDaniel Vetter 		return;
607ea497bb9SDaniel Vetter 
608ea497bb9SDaniel Vetter 	llist_add(&conn->free_node, &config->connector_free_list);
609ea497bb9SDaniel Vetter 	schedule_work(&config->connector_free_work);
610a703c550SDaniel Vetter }
611a703c550SDaniel Vetter 
612613051daSDaniel Vetter /**
613613051daSDaniel Vetter  * drm_connector_list_iter_next - return next connector
6144f45c778SLyude Paul  * @iter: connector_list iterator
615613051daSDaniel Vetter  *
616613051daSDaniel Vetter  * Returns the next connector for @iter, or NULL when the list walk has
617613051daSDaniel Vetter  * completed.
618613051daSDaniel Vetter  */
619613051daSDaniel Vetter struct drm_connector *
620613051daSDaniel Vetter drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
621613051daSDaniel Vetter {
622613051daSDaniel Vetter 	struct drm_connector *old_conn = iter->conn;
623613051daSDaniel Vetter 	struct drm_mode_config *config = &iter->dev->mode_config;
624613051daSDaniel Vetter 	struct list_head *lhead;
625613051daSDaniel Vetter 	unsigned long flags;
626613051daSDaniel Vetter 
627613051daSDaniel Vetter 	spin_lock_irqsave(&config->connector_list_lock, flags);
628613051daSDaniel Vetter 	lhead = old_conn ? &old_conn->head : &config->connector_list;
629613051daSDaniel Vetter 
630613051daSDaniel Vetter 	do {
631613051daSDaniel Vetter 		if (lhead->next == &config->connector_list) {
632613051daSDaniel Vetter 			iter->conn = NULL;
633613051daSDaniel Vetter 			break;
634613051daSDaniel Vetter 		}
635613051daSDaniel Vetter 
636613051daSDaniel Vetter 		lhead = lhead->next;
637613051daSDaniel Vetter 		iter->conn = list_entry(lhead, struct drm_connector, head);
638613051daSDaniel Vetter 
639613051daSDaniel Vetter 		/* loop until it's not a zombie connector */
640613051daSDaniel Vetter 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
641613051daSDaniel Vetter 
642613051daSDaniel Vetter 	if (old_conn)
643ea497bb9SDaniel Vetter 		__drm_connector_put_safe(old_conn);
644ea497bb9SDaniel Vetter 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
645613051daSDaniel Vetter 
646613051daSDaniel Vetter 	return iter->conn;
647613051daSDaniel Vetter }
648613051daSDaniel Vetter EXPORT_SYMBOL(drm_connector_list_iter_next);
649613051daSDaniel Vetter 
650613051daSDaniel Vetter /**
651b982dab1SThierry Reding  * drm_connector_list_iter_end - tear down a connector_list iterator
652613051daSDaniel Vetter  * @iter: connector_list iterator
653613051daSDaniel Vetter  *
654613051daSDaniel Vetter  * Tears down @iter and releases any resources (like &drm_connector references)
655613051daSDaniel Vetter  * acquired while walking the list. This must always be called, both when the
656613051daSDaniel Vetter  * iteration completes fully or when it was aborted without walking the entire
657613051daSDaniel Vetter  * list.
658613051daSDaniel Vetter  */
659b982dab1SThierry Reding void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
660613051daSDaniel Vetter {
661ea497bb9SDaniel Vetter 	struct drm_mode_config *config = &iter->dev->mode_config;
662ea497bb9SDaniel Vetter 	unsigned long flags;
663ea497bb9SDaniel Vetter 
664613051daSDaniel Vetter 	iter->dev = NULL;
665ea497bb9SDaniel Vetter 	if (iter->conn) {
666ea497bb9SDaniel Vetter 		spin_lock_irqsave(&config->connector_list_lock, flags);
667ea497bb9SDaniel Vetter 		__drm_connector_put_safe(iter->conn);
668ea497bb9SDaniel Vetter 		spin_unlock_irqrestore(&config->connector_list_lock, flags);
669ea497bb9SDaniel Vetter 	}
670613051daSDaniel Vetter 	lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
671613051daSDaniel Vetter }
672b982dab1SThierry Reding EXPORT_SYMBOL(drm_connector_list_iter_end);
673613051daSDaniel Vetter 
67452217195SDaniel Vetter static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
67552217195SDaniel Vetter 	{ SubPixelUnknown, "Unknown" },
67652217195SDaniel Vetter 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
67752217195SDaniel Vetter 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
67852217195SDaniel Vetter 	{ SubPixelVerticalRGB, "Vertical RGB" },
67952217195SDaniel Vetter 	{ SubPixelVerticalBGR, "Vertical BGR" },
68052217195SDaniel Vetter 	{ SubPixelNone, "None" },
68152217195SDaniel Vetter };
68252217195SDaniel Vetter 
68352217195SDaniel Vetter /**
68452217195SDaniel Vetter  * drm_get_subpixel_order_name - return a string for a given subpixel enum
68552217195SDaniel Vetter  * @order: enum of subpixel_order
68652217195SDaniel Vetter  *
68752217195SDaniel Vetter  * Note you could abuse this and return something out of bounds, but that
68852217195SDaniel Vetter  * would be a caller error.  No unscrubbed user data should make it here.
68952217195SDaniel Vetter  */
69052217195SDaniel Vetter const char *drm_get_subpixel_order_name(enum subpixel_order order)
69152217195SDaniel Vetter {
69252217195SDaniel Vetter 	return drm_subpixel_enum_list[order].name;
69352217195SDaniel Vetter }
69452217195SDaniel Vetter EXPORT_SYMBOL(drm_get_subpixel_order_name);
69552217195SDaniel Vetter 
69652217195SDaniel Vetter static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
69752217195SDaniel Vetter 	{ DRM_MODE_DPMS_ON, "On" },
69852217195SDaniel Vetter 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
69952217195SDaniel Vetter 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
70052217195SDaniel Vetter 	{ DRM_MODE_DPMS_OFF, "Off" }
70152217195SDaniel Vetter };
70252217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
70352217195SDaniel Vetter 
70440ee6fbeSManasi Navare static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
70540ee6fbeSManasi Navare 	{ DRM_MODE_LINK_STATUS_GOOD, "Good" },
70640ee6fbeSManasi Navare 	{ DRM_MODE_LINK_STATUS_BAD, "Bad" },
70740ee6fbeSManasi Navare };
70840ee6fbeSManasi Navare 
709b3c6c8bfSDaniel Vetter /**
710b3c6c8bfSDaniel Vetter  * drm_display_info_set_bus_formats - set the supported bus formats
711b3c6c8bfSDaniel Vetter  * @info: display info to store bus formats in
712b3c6c8bfSDaniel Vetter  * @formats: array containing the supported bus formats
713b3c6c8bfSDaniel Vetter  * @num_formats: the number of entries in the fmts array
714b3c6c8bfSDaniel Vetter  *
715b3c6c8bfSDaniel Vetter  * Store the supported bus formats in display info structure.
716b3c6c8bfSDaniel Vetter  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
717b3c6c8bfSDaniel Vetter  * a full list of available formats.
718b3c6c8bfSDaniel Vetter  */
719b3c6c8bfSDaniel Vetter int drm_display_info_set_bus_formats(struct drm_display_info *info,
720b3c6c8bfSDaniel Vetter 				     const u32 *formats,
721b3c6c8bfSDaniel Vetter 				     unsigned int num_formats)
722b3c6c8bfSDaniel Vetter {
723b3c6c8bfSDaniel Vetter 	u32 *fmts = NULL;
724b3c6c8bfSDaniel Vetter 
725b3c6c8bfSDaniel Vetter 	if (!formats && num_formats)
726b3c6c8bfSDaniel Vetter 		return -EINVAL;
727b3c6c8bfSDaniel Vetter 
728b3c6c8bfSDaniel Vetter 	if (formats && num_formats) {
729b3c6c8bfSDaniel Vetter 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
730b3c6c8bfSDaniel Vetter 			       GFP_KERNEL);
731b3c6c8bfSDaniel Vetter 		if (!fmts)
732b3c6c8bfSDaniel Vetter 			return -ENOMEM;
733b3c6c8bfSDaniel Vetter 	}
734b3c6c8bfSDaniel Vetter 
735b3c6c8bfSDaniel Vetter 	kfree(info->bus_formats);
736b3c6c8bfSDaniel Vetter 	info->bus_formats = fmts;
737b3c6c8bfSDaniel Vetter 	info->num_bus_formats = num_formats;
738b3c6c8bfSDaniel Vetter 
739b3c6c8bfSDaniel Vetter 	return 0;
740b3c6c8bfSDaniel Vetter }
741b3c6c8bfSDaniel Vetter EXPORT_SYMBOL(drm_display_info_set_bus_formats);
742b3c6c8bfSDaniel Vetter 
74352217195SDaniel Vetter /* Optional connector properties. */
74452217195SDaniel Vetter static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
74552217195SDaniel Vetter 	{ DRM_MODE_SCALE_NONE, "None" },
74652217195SDaniel Vetter 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
74752217195SDaniel Vetter 	{ DRM_MODE_SCALE_CENTER, "Center" },
74852217195SDaniel Vetter 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
74952217195SDaniel Vetter };
75052217195SDaniel Vetter 
75152217195SDaniel Vetter static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
75252217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
75352217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
75452217195SDaniel Vetter 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
75552217195SDaniel Vetter };
75652217195SDaniel Vetter 
75750525c33SStanislav Lisovskiy static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
75850525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
75950525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
76050525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
76150525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
76250525c33SStanislav Lisovskiy 	{ DRM_MODE_CONTENT_TYPE_GAME, "Game" },
76350525c33SStanislav Lisovskiy };
76450525c33SStanislav Lisovskiy 
7658d70f395SHans de Goede static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
7668d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_NORMAL,	"Normal"	},
7678d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP,	"Upside Down"	},
7688d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_LEFT_UP,	"Left Side Up"	},
7698d70f395SHans de Goede 	{ DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,	"Right Side Up"	},
7708d70f395SHans de Goede };
7718d70f395SHans de Goede 
77252217195SDaniel Vetter static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
77352217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
77452217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
77552217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
77652217195SDaniel Vetter };
77752217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
77852217195SDaniel Vetter 
77952217195SDaniel Vetter static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
78052217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
78152217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
78252217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
78352217195SDaniel Vetter };
78452217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
78552217195SDaniel Vetter 		 drm_dvi_i_subconnector_enum_list)
78652217195SDaniel Vetter 
78752217195SDaniel Vetter static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
78852217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
78952217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
79052217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
79152217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
79252217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
79352217195SDaniel Vetter };
79452217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
79552217195SDaniel Vetter 
79652217195SDaniel Vetter static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
79752217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
79852217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
79952217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
80052217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
80152217195SDaniel Vetter 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
80252217195SDaniel Vetter };
80352217195SDaniel Vetter DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
80452217195SDaniel Vetter 		 drm_tv_subconnector_enum_list)
80552217195SDaniel Vetter 
80624557865SSean Paul static struct drm_prop_enum_list drm_cp_enum_list[] = {
80724557865SSean Paul 	{ DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
80824557865SSean Paul 	{ DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
80924557865SSean Paul 	{ DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
81024557865SSean Paul };
81124557865SSean Paul DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
81224557865SSean Paul 
8134ada6f22SDaniel Vetter /**
8144ada6f22SDaniel Vetter  * DOC: standard connector properties
8154ada6f22SDaniel Vetter  *
8164ada6f22SDaniel Vetter  * DRM connectors have a few standardized properties:
8174ada6f22SDaniel Vetter  *
8184ada6f22SDaniel Vetter  * EDID:
8194ada6f22SDaniel Vetter  * 	Blob property which contains the current EDID read from the sink. This
8204ada6f22SDaniel Vetter  * 	is useful to parse sink identification information like vendor, model
8214ada6f22SDaniel Vetter  * 	and serial. Drivers should update this property by calling
822c555f023SDaniel Vetter  * 	drm_connector_update_edid_property(), usually after having parsed
8234ada6f22SDaniel Vetter  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
8244ada6f22SDaniel Vetter  * 	property.
8254ada6f22SDaniel Vetter  * DPMS:
8264ada6f22SDaniel Vetter  * 	Legacy property for setting the power state of the connector. For atomic
8274ada6f22SDaniel Vetter  * 	drivers this is only provided for backwards compatibility with existing
8284ada6f22SDaniel Vetter  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
8294ada6f22SDaniel Vetter  * 	connector is linked to. Drivers should never set this property directly,
830d574528aSDaniel Vetter  * 	it is handled by the DRM core by calling the &drm_connector_funcs.dpms
831144a7999SDaniel Vetter  * 	callback. For atomic drivers the remapping to the "ACTIVE" property is
832144a7999SDaniel Vetter  * 	implemented in the DRM core.  This is the only standard connector
833144a7999SDaniel Vetter  * 	property that userspace can change.
834d0d1aee5SDaniel Vetter  *
835d0d1aee5SDaniel Vetter  * 	Note that this property cannot be set through the MODE_ATOMIC ioctl,
836d0d1aee5SDaniel Vetter  * 	userspace must use "ACTIVE" on the CRTC instead.
837d0d1aee5SDaniel Vetter  *
838d0d1aee5SDaniel Vetter  * 	WARNING:
839d0d1aee5SDaniel Vetter  *
840d0d1aee5SDaniel Vetter  * 	For userspace also running on legacy drivers the "DPMS" semantics are a
841d0d1aee5SDaniel Vetter  * 	lot more complicated. First, userspace cannot rely on the "DPMS" value
842d0d1aee5SDaniel Vetter  * 	returned by the GETCONNECTOR actually reflecting reality, because many
843d0d1aee5SDaniel Vetter  * 	drivers fail to update it. For atomic drivers this is taken care of in
844d0d1aee5SDaniel Vetter  * 	drm_atomic_helper_update_legacy_modeset_state().
845d0d1aee5SDaniel Vetter  *
846d0d1aee5SDaniel Vetter  * 	The second issue is that the DPMS state is only well-defined when the
847d0d1aee5SDaniel Vetter  * 	connector is connected to a CRTC. In atomic the DRM core enforces that
848d0d1aee5SDaniel Vetter  * 	"ACTIVE" is off in such a case, no such checks exists for "DPMS".
849d0d1aee5SDaniel Vetter  *
850d0d1aee5SDaniel Vetter  * 	Finally, when enabling an output using the legacy SETCONFIG ioctl then
851d0d1aee5SDaniel Vetter  * 	"DPMS" is forced to ON. But see above, that might not be reflected in
852d0d1aee5SDaniel Vetter  * 	the software value on legacy drivers.
853d0d1aee5SDaniel Vetter  *
854d0d1aee5SDaniel Vetter  * 	Summarizing: Only set "DPMS" when the connector is known to be enabled,
855d0d1aee5SDaniel Vetter  * 	assume that a successful SETCONFIG call also sets "DPMS" to on, and
856d0d1aee5SDaniel Vetter  * 	never read back the value of "DPMS" because it can be incorrect.
8574ada6f22SDaniel Vetter  * PATH:
8584ada6f22SDaniel Vetter  * 	Connector path property to identify how this sink is physically
8594ada6f22SDaniel Vetter  * 	connected. Used by DP MST. This should be set by calling
86097e14fbeSDaniel Vetter  * 	drm_connector_set_path_property(), in the case of DP MST with the
8614ada6f22SDaniel Vetter  * 	path property the MST manager created. Userspace cannot change this
8624ada6f22SDaniel Vetter  * 	property.
8634ada6f22SDaniel Vetter  * TILE:
8644ada6f22SDaniel Vetter  * 	Connector tile group property to indicate how a set of DRM connector
8654ada6f22SDaniel Vetter  * 	compose together into one logical screen. This is used by both high-res
8664ada6f22SDaniel Vetter  * 	external screens (often only using a single cable, but exposing multiple
8674ada6f22SDaniel Vetter  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
8684ada6f22SDaniel Vetter  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
8694ada6f22SDaniel Vetter  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
8704ada6f22SDaniel Vetter  * 	tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
87197e14fbeSDaniel Vetter  * 	should update this value using drm_connector_set_tile_property().
8724ada6f22SDaniel Vetter  * 	Userspace cannot change this property.
87340ee6fbeSManasi Navare  * link-status:
874716719a3SSean Paul  *      Connector link-status property to indicate the status of link. The
875716719a3SSean Paul  *      default value of link-status is "GOOD". If something fails during or
876716719a3SSean Paul  *      after modeset, the kernel driver may set this to "BAD" and issue a
877716719a3SSean Paul  *      hotplug uevent. Drivers should update this value using
87897e14fbeSDaniel Vetter  *      drm_connector_set_link_status_property().
87966660d4cSDave Airlie  * non_desktop:
88066660d4cSDave Airlie  * 	Indicates the output should be ignored for purposes of displaying a
88166660d4cSDave Airlie  * 	standard desktop environment or console. This is most likely because
88266660d4cSDave Airlie  * 	the output device is not rectilinear.
88324557865SSean Paul  * Content Protection:
88424557865SSean Paul  *	This property is used by userspace to request the kernel protect future
88524557865SSean Paul  *	content communicated over the link. When requested, kernel will apply
88624557865SSean Paul  *	the appropriate means of protection (most often HDCP), and use the
88724557865SSean Paul  *	property to tell userspace the protection is active.
88824557865SSean Paul  *
88924557865SSean Paul  *	Drivers can set this up by calling
89024557865SSean Paul  *	drm_connector_attach_content_protection_property() on initialization.
89124557865SSean Paul  *
89224557865SSean Paul  *	The value of this property can be one of the following:
89324557865SSean Paul  *
894bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
89524557865SSean Paul  *		The link is not protected, content is transmitted in the clear.
896bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
89724557865SSean Paul  *		Userspace has requested content protection, but the link is not
89824557865SSean Paul  *		currently protected. When in this state, kernel should enable
89924557865SSean Paul  *		Content Protection as soon as possible.
900bbeba09fSDaniel Vetter  *	DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
90124557865SSean Paul  *		Userspace has requested content protection, and the link is
90224557865SSean Paul  *		protected. Only the driver can set the property to this value.
90324557865SSean Paul  *		If userspace attempts to set to ENABLED, kernel will return
90424557865SSean Paul  *		-EINVAL.
90524557865SSean Paul  *
90624557865SSean Paul  *	A few guidelines:
90724557865SSean Paul  *
90824557865SSean Paul  *	- DESIRED state should be preserved until userspace de-asserts it by
90924557865SSean Paul  *	  setting the property to UNDESIRED. This means ENABLED should only
91024557865SSean Paul  *	  transition to UNDESIRED when the user explicitly requests it.
91124557865SSean Paul  *	- If the state is DESIRED, kernel should attempt to re-authenticate the
91224557865SSean Paul  *	  link whenever possible. This includes across disable/enable, dpms,
91324557865SSean Paul  *	  hotplug, downstream device changes, link status failures, etc..
91424557865SSean Paul  *	- Userspace is responsible for polling the property to determine when
91524557865SSean Paul  *	  the value transitions from ENABLED to DESIRED. This signifies the link
91624557865SSean Paul  *	  is no longer protected and userspace should take appropriate action
91724557865SSean Paul  *	  (whatever that might be).
9184ada6f22SDaniel Vetter  *
9194ada6f22SDaniel Vetter  * Connectors also have one standardized atomic property:
9204ada6f22SDaniel Vetter  *
9214ada6f22SDaniel Vetter  * CRTC_ID:
9224ada6f22SDaniel Vetter  * 	Mode object ID of the &drm_crtc this connector should be connected to.
9238d70f395SHans de Goede  *
9248d70f395SHans de Goede  * Connectors for LCD panels may also have one standardized property:
9258d70f395SHans de Goede  *
9268d70f395SHans de Goede  * panel orientation:
9278d70f395SHans de Goede  *	On some devices the LCD panel is mounted in the casing in such a way
9288d70f395SHans de Goede  *	that the up/top side of the panel does not match with the top side of
9298d70f395SHans de Goede  *	the device. Userspace can use this property to check for this.
9308d70f395SHans de Goede  *	Note that input coordinates from touchscreens (input devices with
9318d70f395SHans de Goede  *	INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
9328d70f395SHans de Goede  *	coordinates, so if userspace rotates the picture to adjust for
9338d70f395SHans de Goede  *	the orientation it must also apply the same transformation to the
934bbeba09fSDaniel Vetter  *	touchscreen input coordinates. This property is initialized by calling
935bbeba09fSDaniel Vetter  *	drm_connector_init_panel_orientation_property().
936bbeba09fSDaniel Vetter  *
937bbeba09fSDaniel Vetter  * scaling mode:
938bbeba09fSDaniel Vetter  *	This property defines how a non-native mode is upscaled to the native
939bbeba09fSDaniel Vetter  *	mode of an LCD panel:
940bbeba09fSDaniel Vetter  *
941bbeba09fSDaniel Vetter  *	None:
942bbeba09fSDaniel Vetter  *		No upscaling happens, scaling is left to the panel. Not all
943bbeba09fSDaniel Vetter  *		drivers expose this mode.
944bbeba09fSDaniel Vetter  *	Full:
945bbeba09fSDaniel Vetter  *		The output is upscaled to the full resolution of the panel,
946bbeba09fSDaniel Vetter  *		ignoring the aspect ratio.
947bbeba09fSDaniel Vetter  *	Center:
948bbeba09fSDaniel Vetter  *		No upscaling happens, the output is centered within the native
949bbeba09fSDaniel Vetter  *		resolution the panel.
950bbeba09fSDaniel Vetter  *	Full aspect:
951bbeba09fSDaniel Vetter  *		The output is upscaled to maximize either the width or height
952bbeba09fSDaniel Vetter  *		while retaining the aspect ratio.
953bbeba09fSDaniel Vetter  *
954bbeba09fSDaniel Vetter  *	This property should be set up by calling
955bbeba09fSDaniel Vetter  *	drm_connector_attach_scaling_mode_property(). Note that drivers
956bbeba09fSDaniel Vetter  *	can also expose this property to external outputs, in which case they
957bbeba09fSDaniel Vetter  *	must support "None", which should be the default (since external screens
958bbeba09fSDaniel Vetter  *	have a built-in scaler).
9594ada6f22SDaniel Vetter  */
9604ada6f22SDaniel Vetter 
96152217195SDaniel Vetter int drm_connector_create_standard_properties(struct drm_device *dev)
96252217195SDaniel Vetter {
96352217195SDaniel Vetter 	struct drm_property *prop;
96452217195SDaniel Vetter 
96552217195SDaniel Vetter 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
96652217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
96752217195SDaniel Vetter 				   "EDID", 0);
96852217195SDaniel Vetter 	if (!prop)
96952217195SDaniel Vetter 		return -ENOMEM;
97052217195SDaniel Vetter 	dev->mode_config.edid_property = prop;
97152217195SDaniel Vetter 
97252217195SDaniel Vetter 	prop = drm_property_create_enum(dev, 0,
97352217195SDaniel Vetter 				   "DPMS", drm_dpms_enum_list,
97452217195SDaniel Vetter 				   ARRAY_SIZE(drm_dpms_enum_list));
97552217195SDaniel Vetter 	if (!prop)
97652217195SDaniel Vetter 		return -ENOMEM;
97752217195SDaniel Vetter 	dev->mode_config.dpms_property = prop;
97852217195SDaniel Vetter 
97952217195SDaniel Vetter 	prop = drm_property_create(dev,
98052217195SDaniel Vetter 				   DRM_MODE_PROP_BLOB |
98152217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
98252217195SDaniel Vetter 				   "PATH", 0);
98352217195SDaniel Vetter 	if (!prop)
98452217195SDaniel Vetter 		return -ENOMEM;
98552217195SDaniel Vetter 	dev->mode_config.path_property = prop;
98652217195SDaniel Vetter 
98752217195SDaniel Vetter 	prop = drm_property_create(dev,
98852217195SDaniel Vetter 				   DRM_MODE_PROP_BLOB |
98952217195SDaniel Vetter 				   DRM_MODE_PROP_IMMUTABLE,
99052217195SDaniel Vetter 				   "TILE", 0);
99152217195SDaniel Vetter 	if (!prop)
99252217195SDaniel Vetter 		return -ENOMEM;
99352217195SDaniel Vetter 	dev->mode_config.tile_property = prop;
99452217195SDaniel Vetter 
99540ee6fbeSManasi Navare 	prop = drm_property_create_enum(dev, 0, "link-status",
99640ee6fbeSManasi Navare 					drm_link_status_enum_list,
99740ee6fbeSManasi Navare 					ARRAY_SIZE(drm_link_status_enum_list));
99840ee6fbeSManasi Navare 	if (!prop)
99940ee6fbeSManasi Navare 		return -ENOMEM;
100040ee6fbeSManasi Navare 	dev->mode_config.link_status_property = prop;
100140ee6fbeSManasi Navare 
100266660d4cSDave Airlie 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
100366660d4cSDave Airlie 	if (!prop)
100466660d4cSDave Airlie 		return -ENOMEM;
100566660d4cSDave Airlie 	dev->mode_config.non_desktop_property = prop;
100666660d4cSDave Airlie 
100752217195SDaniel Vetter 	return 0;
100852217195SDaniel Vetter }
100952217195SDaniel Vetter 
101052217195SDaniel Vetter /**
101152217195SDaniel Vetter  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
101252217195SDaniel Vetter  * @dev: DRM device
101352217195SDaniel Vetter  *
101452217195SDaniel Vetter  * Called by a driver the first time a DVI-I connector is made.
101552217195SDaniel Vetter  */
101652217195SDaniel Vetter int drm_mode_create_dvi_i_properties(struct drm_device *dev)
101752217195SDaniel Vetter {
101852217195SDaniel Vetter 	struct drm_property *dvi_i_selector;
101952217195SDaniel Vetter 	struct drm_property *dvi_i_subconnector;
102052217195SDaniel Vetter 
102152217195SDaniel Vetter 	if (dev->mode_config.dvi_i_select_subconnector_property)
102252217195SDaniel Vetter 		return 0;
102352217195SDaniel Vetter 
102452217195SDaniel Vetter 	dvi_i_selector =
102552217195SDaniel Vetter 		drm_property_create_enum(dev, 0,
102652217195SDaniel Vetter 				    "select subconnector",
102752217195SDaniel Vetter 				    drm_dvi_i_select_enum_list,
102852217195SDaniel Vetter 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
102952217195SDaniel Vetter 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
103052217195SDaniel Vetter 
103152217195SDaniel Vetter 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
103252217195SDaniel Vetter 				    "subconnector",
103352217195SDaniel Vetter 				    drm_dvi_i_subconnector_enum_list,
103452217195SDaniel Vetter 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
103552217195SDaniel Vetter 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
103652217195SDaniel Vetter 
103752217195SDaniel Vetter 	return 0;
103852217195SDaniel Vetter }
103952217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
104052217195SDaniel Vetter 
104152217195SDaniel Vetter /**
104250525c33SStanislav Lisovskiy  * DOC: HDMI connector properties
104350525c33SStanislav Lisovskiy  *
104450525c33SStanislav Lisovskiy  * content type (HDMI specific):
104550525c33SStanislav Lisovskiy  *	Indicates content type setting to be used in HDMI infoframes to indicate
104650525c33SStanislav Lisovskiy  *	content type for the external device, so that it adjusts it's display
104750525c33SStanislav Lisovskiy  *	settings accordingly.
104850525c33SStanislav Lisovskiy  *
104950525c33SStanislav Lisovskiy  *	The value of this property can be one of the following:
105050525c33SStanislav Lisovskiy  *
105150525c33SStanislav Lisovskiy  *	No Data:
105250525c33SStanislav Lisovskiy  *		Content type is unknown
105350525c33SStanislav Lisovskiy  *	Graphics:
105450525c33SStanislav Lisovskiy  *		Content type is graphics
105550525c33SStanislav Lisovskiy  *	Photo:
105650525c33SStanislav Lisovskiy  *		Content type is photo
105750525c33SStanislav Lisovskiy  *	Cinema:
105850525c33SStanislav Lisovskiy  *		Content type is cinema
105950525c33SStanislav Lisovskiy  *	Game:
106050525c33SStanislav Lisovskiy  *		Content type is game
106150525c33SStanislav Lisovskiy  *
106250525c33SStanislav Lisovskiy  *	Drivers can set up this property by calling
106350525c33SStanislav Lisovskiy  *	drm_connector_attach_content_type_property(). Decoding to
1064ba609631SDaniel Vetter  *	infoframe values is done through drm_hdmi_avi_infoframe_content_type().
106550525c33SStanislav Lisovskiy  */
106650525c33SStanislav Lisovskiy 
106750525c33SStanislav Lisovskiy /**
106850525c33SStanislav Lisovskiy  * drm_connector_attach_content_type_property - attach content-type property
106950525c33SStanislav Lisovskiy  * @connector: connector to attach content type property on.
107050525c33SStanislav Lisovskiy  *
107150525c33SStanislav Lisovskiy  * Called by a driver the first time a HDMI connector is made.
107250525c33SStanislav Lisovskiy  */
107350525c33SStanislav Lisovskiy int drm_connector_attach_content_type_property(struct drm_connector *connector)
107450525c33SStanislav Lisovskiy {
107550525c33SStanislav Lisovskiy 	if (!drm_mode_create_content_type_property(connector->dev))
107650525c33SStanislav Lisovskiy 		drm_object_attach_property(&connector->base,
107750525c33SStanislav Lisovskiy 					   connector->dev->mode_config.content_type_property,
107850525c33SStanislav Lisovskiy 					   DRM_MODE_CONTENT_TYPE_NO_DATA);
107950525c33SStanislav Lisovskiy 	return 0;
108050525c33SStanislav Lisovskiy }
108150525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_connector_attach_content_type_property);
108250525c33SStanislav Lisovskiy 
108350525c33SStanislav Lisovskiy 
108450525c33SStanislav Lisovskiy /**
108550525c33SStanislav Lisovskiy  * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
108650525c33SStanislav Lisovskiy  *                                         content type information, based
108750525c33SStanislav Lisovskiy  *                                         on correspondent DRM property.
108850525c33SStanislav Lisovskiy  * @frame: HDMI AVI infoframe
108950525c33SStanislav Lisovskiy  * @conn_state: DRM display connector state
109050525c33SStanislav Lisovskiy  *
109150525c33SStanislav Lisovskiy  */
109250525c33SStanislav Lisovskiy void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
109350525c33SStanislav Lisovskiy 					 const struct drm_connector_state *conn_state)
109450525c33SStanislav Lisovskiy {
109550525c33SStanislav Lisovskiy 	switch (conn_state->content_type) {
109650525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_GRAPHICS:
109750525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
109850525c33SStanislav Lisovskiy 		break;
109950525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_CINEMA:
110050525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
110150525c33SStanislav Lisovskiy 		break;
110250525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_GAME:
110350525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GAME;
110450525c33SStanislav Lisovskiy 		break;
110550525c33SStanislav Lisovskiy 	case DRM_MODE_CONTENT_TYPE_PHOTO:
110650525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
110750525c33SStanislav Lisovskiy 		break;
110850525c33SStanislav Lisovskiy 	default:
110950525c33SStanislav Lisovskiy 		/* Graphics is the default(0) */
111050525c33SStanislav Lisovskiy 		frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
111150525c33SStanislav Lisovskiy 	}
111250525c33SStanislav Lisovskiy 
111350525c33SStanislav Lisovskiy 	frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
111450525c33SStanislav Lisovskiy }
111550525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
111650525c33SStanislav Lisovskiy 
111750525c33SStanislav Lisovskiy /**
111852217195SDaniel Vetter  * drm_create_tv_properties - create TV specific connector properties
111952217195SDaniel Vetter  * @dev: DRM device
112052217195SDaniel Vetter  * @num_modes: number of different TV formats (modes) supported
112152217195SDaniel Vetter  * @modes: array of pointers to strings containing name of each format
112252217195SDaniel Vetter  *
112352217195SDaniel Vetter  * Called by a driver's TV initialization routine, this function creates
112452217195SDaniel Vetter  * the TV specific connector properties for a given device.  Caller is
112552217195SDaniel Vetter  * responsible for allocating a list of format names and passing them to
112652217195SDaniel Vetter  * this routine.
112752217195SDaniel Vetter  */
112852217195SDaniel Vetter int drm_mode_create_tv_properties(struct drm_device *dev,
112952217195SDaniel Vetter 				  unsigned int num_modes,
113052217195SDaniel Vetter 				  const char * const modes[])
113152217195SDaniel Vetter {
113252217195SDaniel Vetter 	struct drm_property *tv_selector;
113352217195SDaniel Vetter 	struct drm_property *tv_subconnector;
113452217195SDaniel Vetter 	unsigned int i;
113552217195SDaniel Vetter 
113652217195SDaniel Vetter 	if (dev->mode_config.tv_select_subconnector_property)
113752217195SDaniel Vetter 		return 0;
113852217195SDaniel Vetter 
113952217195SDaniel Vetter 	/*
114052217195SDaniel Vetter 	 * Basic connector properties
114152217195SDaniel Vetter 	 */
114252217195SDaniel Vetter 	tv_selector = drm_property_create_enum(dev, 0,
114352217195SDaniel Vetter 					  "select subconnector",
114452217195SDaniel Vetter 					  drm_tv_select_enum_list,
114552217195SDaniel Vetter 					  ARRAY_SIZE(drm_tv_select_enum_list));
114652217195SDaniel Vetter 	if (!tv_selector)
114752217195SDaniel Vetter 		goto nomem;
114852217195SDaniel Vetter 
114952217195SDaniel Vetter 	dev->mode_config.tv_select_subconnector_property = tv_selector;
115052217195SDaniel Vetter 
115152217195SDaniel Vetter 	tv_subconnector =
115252217195SDaniel Vetter 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
115352217195SDaniel Vetter 				    "subconnector",
115452217195SDaniel Vetter 				    drm_tv_subconnector_enum_list,
115552217195SDaniel Vetter 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
115652217195SDaniel Vetter 	if (!tv_subconnector)
115752217195SDaniel Vetter 		goto nomem;
115852217195SDaniel Vetter 	dev->mode_config.tv_subconnector_property = tv_subconnector;
115952217195SDaniel Vetter 
116052217195SDaniel Vetter 	/*
116152217195SDaniel Vetter 	 * Other, TV specific properties: margins & TV modes.
116252217195SDaniel Vetter 	 */
116352217195SDaniel Vetter 	dev->mode_config.tv_left_margin_property =
116452217195SDaniel Vetter 		drm_property_create_range(dev, 0, "left margin", 0, 100);
116552217195SDaniel Vetter 	if (!dev->mode_config.tv_left_margin_property)
116652217195SDaniel Vetter 		goto nomem;
116752217195SDaniel Vetter 
116852217195SDaniel Vetter 	dev->mode_config.tv_right_margin_property =
116952217195SDaniel Vetter 		drm_property_create_range(dev, 0, "right margin", 0, 100);
117052217195SDaniel Vetter 	if (!dev->mode_config.tv_right_margin_property)
117152217195SDaniel Vetter 		goto nomem;
117252217195SDaniel Vetter 
117352217195SDaniel Vetter 	dev->mode_config.tv_top_margin_property =
117452217195SDaniel Vetter 		drm_property_create_range(dev, 0, "top margin", 0, 100);
117552217195SDaniel Vetter 	if (!dev->mode_config.tv_top_margin_property)
117652217195SDaniel Vetter 		goto nomem;
117752217195SDaniel Vetter 
117852217195SDaniel Vetter 	dev->mode_config.tv_bottom_margin_property =
117952217195SDaniel Vetter 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
118052217195SDaniel Vetter 	if (!dev->mode_config.tv_bottom_margin_property)
118152217195SDaniel Vetter 		goto nomem;
118252217195SDaniel Vetter 
118352217195SDaniel Vetter 	dev->mode_config.tv_mode_property =
118452217195SDaniel Vetter 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
118552217195SDaniel Vetter 				    "mode", num_modes);
118652217195SDaniel Vetter 	if (!dev->mode_config.tv_mode_property)
118752217195SDaniel Vetter 		goto nomem;
118852217195SDaniel Vetter 
118952217195SDaniel Vetter 	for (i = 0; i < num_modes; i++)
119030e9db6dSVille Syrjälä 		drm_property_add_enum(dev->mode_config.tv_mode_property,
119152217195SDaniel Vetter 				      i, modes[i]);
119252217195SDaniel Vetter 
119352217195SDaniel Vetter 	dev->mode_config.tv_brightness_property =
119452217195SDaniel Vetter 		drm_property_create_range(dev, 0, "brightness", 0, 100);
119552217195SDaniel Vetter 	if (!dev->mode_config.tv_brightness_property)
119652217195SDaniel Vetter 		goto nomem;
119752217195SDaniel Vetter 
119852217195SDaniel Vetter 	dev->mode_config.tv_contrast_property =
119952217195SDaniel Vetter 		drm_property_create_range(dev, 0, "contrast", 0, 100);
120052217195SDaniel Vetter 	if (!dev->mode_config.tv_contrast_property)
120152217195SDaniel Vetter 		goto nomem;
120252217195SDaniel Vetter 
120352217195SDaniel Vetter 	dev->mode_config.tv_flicker_reduction_property =
120452217195SDaniel Vetter 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
120552217195SDaniel Vetter 	if (!dev->mode_config.tv_flicker_reduction_property)
120652217195SDaniel Vetter 		goto nomem;
120752217195SDaniel Vetter 
120852217195SDaniel Vetter 	dev->mode_config.tv_overscan_property =
120952217195SDaniel Vetter 		drm_property_create_range(dev, 0, "overscan", 0, 100);
121052217195SDaniel Vetter 	if (!dev->mode_config.tv_overscan_property)
121152217195SDaniel Vetter 		goto nomem;
121252217195SDaniel Vetter 
121352217195SDaniel Vetter 	dev->mode_config.tv_saturation_property =
121452217195SDaniel Vetter 		drm_property_create_range(dev, 0, "saturation", 0, 100);
121552217195SDaniel Vetter 	if (!dev->mode_config.tv_saturation_property)
121652217195SDaniel Vetter 		goto nomem;
121752217195SDaniel Vetter 
121852217195SDaniel Vetter 	dev->mode_config.tv_hue_property =
121952217195SDaniel Vetter 		drm_property_create_range(dev, 0, "hue", 0, 100);
122052217195SDaniel Vetter 	if (!dev->mode_config.tv_hue_property)
122152217195SDaniel Vetter 		goto nomem;
122252217195SDaniel Vetter 
122352217195SDaniel Vetter 	return 0;
122452217195SDaniel Vetter nomem:
122552217195SDaniel Vetter 	return -ENOMEM;
122652217195SDaniel Vetter }
122752217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_tv_properties);
122852217195SDaniel Vetter 
122952217195SDaniel Vetter /**
123052217195SDaniel Vetter  * drm_mode_create_scaling_mode_property - create scaling mode property
123152217195SDaniel Vetter  * @dev: DRM device
123252217195SDaniel Vetter  *
123352217195SDaniel Vetter  * Called by a driver the first time it's needed, must be attached to desired
123452217195SDaniel Vetter  * connectors.
12358f6e1e22SMaarten Lankhorst  *
12368f6e1e22SMaarten Lankhorst  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
12378f6e1e22SMaarten Lankhorst  * instead to correctly assign &drm_connector_state.picture_aspect_ratio
12388f6e1e22SMaarten Lankhorst  * in the atomic state.
123952217195SDaniel Vetter  */
124052217195SDaniel Vetter int drm_mode_create_scaling_mode_property(struct drm_device *dev)
124152217195SDaniel Vetter {
124252217195SDaniel Vetter 	struct drm_property *scaling_mode;
124352217195SDaniel Vetter 
124452217195SDaniel Vetter 	if (dev->mode_config.scaling_mode_property)
124552217195SDaniel Vetter 		return 0;
124652217195SDaniel Vetter 
124752217195SDaniel Vetter 	scaling_mode =
124852217195SDaniel Vetter 		drm_property_create_enum(dev, 0, "scaling mode",
124952217195SDaniel Vetter 				drm_scaling_mode_enum_list,
125052217195SDaniel Vetter 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
125152217195SDaniel Vetter 
125252217195SDaniel Vetter 	dev->mode_config.scaling_mode_property = scaling_mode;
125352217195SDaniel Vetter 
125452217195SDaniel Vetter 	return 0;
125552217195SDaniel Vetter }
125652217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
125752217195SDaniel Vetter 
125852217195SDaniel Vetter /**
12598f6e1e22SMaarten Lankhorst  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
12608f6e1e22SMaarten Lankhorst  * @connector: connector to attach scaling mode property on.
12618f6e1e22SMaarten Lankhorst  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
12628f6e1e22SMaarten Lankhorst  *
12638f6e1e22SMaarten Lankhorst  * This is used to add support for scaling mode to atomic drivers.
12648f6e1e22SMaarten Lankhorst  * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
12658f6e1e22SMaarten Lankhorst  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
12668f6e1e22SMaarten Lankhorst  *
12678f6e1e22SMaarten Lankhorst  * This is the atomic version of drm_mode_create_scaling_mode_property().
12688f6e1e22SMaarten Lankhorst  *
12698f6e1e22SMaarten Lankhorst  * Returns:
12708f6e1e22SMaarten Lankhorst  * Zero on success, negative errno on failure.
12718f6e1e22SMaarten Lankhorst  */
12728f6e1e22SMaarten Lankhorst int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
12738f6e1e22SMaarten Lankhorst 					       u32 scaling_mode_mask)
12748f6e1e22SMaarten Lankhorst {
12758f6e1e22SMaarten Lankhorst 	struct drm_device *dev = connector->dev;
12768f6e1e22SMaarten Lankhorst 	struct drm_property *scaling_mode_property;
127730e9db6dSVille Syrjälä 	int i;
12788f6e1e22SMaarten Lankhorst 	const unsigned valid_scaling_mode_mask =
12798f6e1e22SMaarten Lankhorst 		(1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
12808f6e1e22SMaarten Lankhorst 
12818f6e1e22SMaarten Lankhorst 	if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
12828f6e1e22SMaarten Lankhorst 		    scaling_mode_mask & ~valid_scaling_mode_mask))
12838f6e1e22SMaarten Lankhorst 		return -EINVAL;
12848f6e1e22SMaarten Lankhorst 
12858f6e1e22SMaarten Lankhorst 	scaling_mode_property =
12868f6e1e22SMaarten Lankhorst 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
12878f6e1e22SMaarten Lankhorst 				    hweight32(scaling_mode_mask));
12888f6e1e22SMaarten Lankhorst 
12898f6e1e22SMaarten Lankhorst 	if (!scaling_mode_property)
12908f6e1e22SMaarten Lankhorst 		return -ENOMEM;
12918f6e1e22SMaarten Lankhorst 
12928f6e1e22SMaarten Lankhorst 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
12938f6e1e22SMaarten Lankhorst 		int ret;
12948f6e1e22SMaarten Lankhorst 
12958f6e1e22SMaarten Lankhorst 		if (!(BIT(i) & scaling_mode_mask))
12968f6e1e22SMaarten Lankhorst 			continue;
12978f6e1e22SMaarten Lankhorst 
129830e9db6dSVille Syrjälä 		ret = drm_property_add_enum(scaling_mode_property,
12998f6e1e22SMaarten Lankhorst 					    drm_scaling_mode_enum_list[i].type,
13008f6e1e22SMaarten Lankhorst 					    drm_scaling_mode_enum_list[i].name);
13018f6e1e22SMaarten Lankhorst 
13028f6e1e22SMaarten Lankhorst 		if (ret) {
13038f6e1e22SMaarten Lankhorst 			drm_property_destroy(dev, scaling_mode_property);
13048f6e1e22SMaarten Lankhorst 
13058f6e1e22SMaarten Lankhorst 			return ret;
13068f6e1e22SMaarten Lankhorst 		}
13078f6e1e22SMaarten Lankhorst 	}
13088f6e1e22SMaarten Lankhorst 
13098f6e1e22SMaarten Lankhorst 	drm_object_attach_property(&connector->base,
13108f6e1e22SMaarten Lankhorst 				   scaling_mode_property, 0);
13118f6e1e22SMaarten Lankhorst 
13128f6e1e22SMaarten Lankhorst 	connector->scaling_mode_property = scaling_mode_property;
13138f6e1e22SMaarten Lankhorst 
13148f6e1e22SMaarten Lankhorst 	return 0;
13158f6e1e22SMaarten Lankhorst }
13168f6e1e22SMaarten Lankhorst EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
13178f6e1e22SMaarten Lankhorst 
13188f6e1e22SMaarten Lankhorst /**
131924557865SSean Paul  * drm_connector_attach_content_protection_property - attach content protection
132024557865SSean Paul  * property
132124557865SSean Paul  *
132224557865SSean Paul  * @connector: connector to attach CP property on.
132324557865SSean Paul  *
132424557865SSean Paul  * This is used to add support for content protection on select connectors.
132524557865SSean Paul  * Content Protection is intentionally vague to allow for different underlying
132624557865SSean Paul  * technologies, however it is most implemented by HDCP.
132724557865SSean Paul  *
132824557865SSean Paul  * The content protection will be set to &drm_connector_state.content_protection
132924557865SSean Paul  *
133024557865SSean Paul  * Returns:
133124557865SSean Paul  * Zero on success, negative errno on failure.
133224557865SSean Paul  */
133324557865SSean Paul int drm_connector_attach_content_protection_property(
133424557865SSean Paul 		struct drm_connector *connector)
133524557865SSean Paul {
133624557865SSean Paul 	struct drm_device *dev = connector->dev;
133724557865SSean Paul 	struct drm_property *prop;
133824557865SSean Paul 
133924557865SSean Paul 	prop = drm_property_create_enum(dev, 0, "Content Protection",
134024557865SSean Paul 					drm_cp_enum_list,
134124557865SSean Paul 					ARRAY_SIZE(drm_cp_enum_list));
134224557865SSean Paul 	if (!prop)
134324557865SSean Paul 		return -ENOMEM;
134424557865SSean Paul 
134524557865SSean Paul 	drm_object_attach_property(&connector->base, prop,
134624557865SSean Paul 				   DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
134724557865SSean Paul 
134824557865SSean Paul 	connector->content_protection_property = prop;
134924557865SSean Paul 
135024557865SSean Paul 	return 0;
135124557865SSean Paul }
135224557865SSean Paul EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
135324557865SSean Paul 
135424557865SSean Paul /**
135552217195SDaniel Vetter  * drm_mode_create_aspect_ratio_property - create aspect ratio property
135652217195SDaniel Vetter  * @dev: DRM device
135752217195SDaniel Vetter  *
135852217195SDaniel Vetter  * Called by a driver the first time it's needed, must be attached to desired
135952217195SDaniel Vetter  * connectors.
136052217195SDaniel Vetter  *
136152217195SDaniel Vetter  * Returns:
136252217195SDaniel Vetter  * Zero on success, negative errno on failure.
136352217195SDaniel Vetter  */
136452217195SDaniel Vetter int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
136552217195SDaniel Vetter {
136652217195SDaniel Vetter 	if (dev->mode_config.aspect_ratio_property)
136752217195SDaniel Vetter 		return 0;
136852217195SDaniel Vetter 
136952217195SDaniel Vetter 	dev->mode_config.aspect_ratio_property =
137052217195SDaniel Vetter 		drm_property_create_enum(dev, 0, "aspect ratio",
137152217195SDaniel Vetter 				drm_aspect_ratio_enum_list,
137252217195SDaniel Vetter 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
137352217195SDaniel Vetter 
137452217195SDaniel Vetter 	if (dev->mode_config.aspect_ratio_property == NULL)
137552217195SDaniel Vetter 		return -ENOMEM;
137652217195SDaniel Vetter 
137752217195SDaniel Vetter 	return 0;
137852217195SDaniel Vetter }
137952217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
138052217195SDaniel Vetter 
138152217195SDaniel Vetter /**
138250525c33SStanislav Lisovskiy  * drm_mode_create_content_type_property - create content type property
138350525c33SStanislav Lisovskiy  * @dev: DRM device
138450525c33SStanislav Lisovskiy  *
138550525c33SStanislav Lisovskiy  * Called by a driver the first time it's needed, must be attached to desired
138650525c33SStanislav Lisovskiy  * connectors.
138750525c33SStanislav Lisovskiy  *
138850525c33SStanislav Lisovskiy  * Returns:
138950525c33SStanislav Lisovskiy  * Zero on success, negative errno on failure.
139050525c33SStanislav Lisovskiy  */
139150525c33SStanislav Lisovskiy int drm_mode_create_content_type_property(struct drm_device *dev)
139250525c33SStanislav Lisovskiy {
139350525c33SStanislav Lisovskiy 	if (dev->mode_config.content_type_property)
139450525c33SStanislav Lisovskiy 		return 0;
139550525c33SStanislav Lisovskiy 
139650525c33SStanislav Lisovskiy 	dev->mode_config.content_type_property =
139750525c33SStanislav Lisovskiy 		drm_property_create_enum(dev, 0, "content type",
139850525c33SStanislav Lisovskiy 					 drm_content_type_enum_list,
139950525c33SStanislav Lisovskiy 					 ARRAY_SIZE(drm_content_type_enum_list));
140050525c33SStanislav Lisovskiy 
140150525c33SStanislav Lisovskiy 	if (dev->mode_config.content_type_property == NULL)
140250525c33SStanislav Lisovskiy 		return -ENOMEM;
140350525c33SStanislav Lisovskiy 
140450525c33SStanislav Lisovskiy 	return 0;
140550525c33SStanislav Lisovskiy }
140650525c33SStanislav Lisovskiy EXPORT_SYMBOL(drm_mode_create_content_type_property);
140750525c33SStanislav Lisovskiy 
140850525c33SStanislav Lisovskiy /**
140952217195SDaniel Vetter  * drm_mode_create_suggested_offset_properties - create suggests offset properties
141052217195SDaniel Vetter  * @dev: DRM device
141152217195SDaniel Vetter  *
141252217195SDaniel Vetter  * Create the the suggested x/y offset property for connectors.
141352217195SDaniel Vetter  */
141452217195SDaniel Vetter int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
141552217195SDaniel Vetter {
141652217195SDaniel Vetter 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
141752217195SDaniel Vetter 		return 0;
141852217195SDaniel Vetter 
141952217195SDaniel Vetter 	dev->mode_config.suggested_x_property =
142052217195SDaniel Vetter 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
142152217195SDaniel Vetter 
142252217195SDaniel Vetter 	dev->mode_config.suggested_y_property =
142352217195SDaniel Vetter 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
142452217195SDaniel Vetter 
142552217195SDaniel Vetter 	if (dev->mode_config.suggested_x_property == NULL ||
142652217195SDaniel Vetter 	    dev->mode_config.suggested_y_property == NULL)
142752217195SDaniel Vetter 		return -ENOMEM;
142852217195SDaniel Vetter 	return 0;
142952217195SDaniel Vetter }
143052217195SDaniel Vetter EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
143152217195SDaniel Vetter 
143252217195SDaniel Vetter /**
143397e14fbeSDaniel Vetter  * drm_connector_set_path_property - set tile property on connector
143452217195SDaniel Vetter  * @connector: connector to set property on.
143552217195SDaniel Vetter  * @path: path to use for property; must not be NULL.
143652217195SDaniel Vetter  *
143752217195SDaniel Vetter  * This creates a property to expose to userspace to specify a
143852217195SDaniel Vetter  * connector path. This is mainly used for DisplayPort MST where
143952217195SDaniel Vetter  * connectors have a topology and we want to allow userspace to give
144052217195SDaniel Vetter  * them more meaningful names.
144152217195SDaniel Vetter  *
144252217195SDaniel Vetter  * Returns:
144352217195SDaniel Vetter  * Zero on success, negative errno on failure.
144452217195SDaniel Vetter  */
144597e14fbeSDaniel Vetter int drm_connector_set_path_property(struct drm_connector *connector,
144652217195SDaniel Vetter 				    const char *path)
144752217195SDaniel Vetter {
144852217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
144952217195SDaniel Vetter 	int ret;
145052217195SDaniel Vetter 
145152217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
145252217195SDaniel Vetter 	                                       &connector->path_blob_ptr,
145352217195SDaniel Vetter 	                                       strlen(path) + 1,
145452217195SDaniel Vetter 	                                       path,
145552217195SDaniel Vetter 	                                       &connector->base,
145652217195SDaniel Vetter 	                                       dev->mode_config.path_property);
145752217195SDaniel Vetter 	return ret;
145852217195SDaniel Vetter }
145997e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_path_property);
146052217195SDaniel Vetter 
146152217195SDaniel Vetter /**
146297e14fbeSDaniel Vetter  * drm_connector_set_tile_property - set tile property on connector
146352217195SDaniel Vetter  * @connector: connector to set property on.
146452217195SDaniel Vetter  *
146552217195SDaniel Vetter  * This looks up the tile information for a connector, and creates a
146652217195SDaniel Vetter  * property for userspace to parse if it exists. The property is of
146752217195SDaniel Vetter  * the form of 8 integers using ':' as a separator.
146852217195SDaniel Vetter  *
146952217195SDaniel Vetter  * Returns:
147052217195SDaniel Vetter  * Zero on success, errno on failure.
147152217195SDaniel Vetter  */
147297e14fbeSDaniel Vetter int drm_connector_set_tile_property(struct drm_connector *connector)
147352217195SDaniel Vetter {
147452217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
147552217195SDaniel Vetter 	char tile[256];
147652217195SDaniel Vetter 	int ret;
147752217195SDaniel Vetter 
147852217195SDaniel Vetter 	if (!connector->has_tile) {
147952217195SDaniel Vetter 		ret  = drm_property_replace_global_blob(dev,
148052217195SDaniel Vetter 		                                        &connector->tile_blob_ptr,
148152217195SDaniel Vetter 		                                        0,
148252217195SDaniel Vetter 		                                        NULL,
148352217195SDaniel Vetter 		                                        &connector->base,
148452217195SDaniel Vetter 		                                        dev->mode_config.tile_property);
148552217195SDaniel Vetter 		return ret;
148652217195SDaniel Vetter 	}
148752217195SDaniel Vetter 
148852217195SDaniel Vetter 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
148952217195SDaniel Vetter 		 connector->tile_group->id, connector->tile_is_single_monitor,
149052217195SDaniel Vetter 		 connector->num_h_tile, connector->num_v_tile,
149152217195SDaniel Vetter 		 connector->tile_h_loc, connector->tile_v_loc,
149252217195SDaniel Vetter 		 connector->tile_h_size, connector->tile_v_size);
149352217195SDaniel Vetter 
149452217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
149552217195SDaniel Vetter 	                                       &connector->tile_blob_ptr,
149652217195SDaniel Vetter 	                                       strlen(tile) + 1,
149752217195SDaniel Vetter 	                                       tile,
149852217195SDaniel Vetter 	                                       &connector->base,
149952217195SDaniel Vetter 	                                       dev->mode_config.tile_property);
150052217195SDaniel Vetter 	return ret;
150152217195SDaniel Vetter }
150297e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_tile_property);
150352217195SDaniel Vetter 
150452217195SDaniel Vetter /**
1505c555f023SDaniel Vetter  * drm_connector_update_edid_property - update the edid property of a connector
150652217195SDaniel Vetter  * @connector: drm connector
150752217195SDaniel Vetter  * @edid: new value of the edid property
150852217195SDaniel Vetter  *
150952217195SDaniel Vetter  * This function creates a new blob modeset object and assigns its id to the
151052217195SDaniel Vetter  * connector's edid property.
151152217195SDaniel Vetter  *
151252217195SDaniel Vetter  * Returns:
151352217195SDaniel Vetter  * Zero on success, negative errno on failure.
151452217195SDaniel Vetter  */
1515c555f023SDaniel Vetter int drm_connector_update_edid_property(struct drm_connector *connector,
151652217195SDaniel Vetter 				       const struct edid *edid)
151752217195SDaniel Vetter {
151852217195SDaniel Vetter 	struct drm_device *dev = connector->dev;
151952217195SDaniel Vetter 	size_t size = 0;
152052217195SDaniel Vetter 	int ret;
152152217195SDaniel Vetter 
152252217195SDaniel Vetter 	/* ignore requests to set edid when overridden */
152352217195SDaniel Vetter 	if (connector->override_edid)
152452217195SDaniel Vetter 		return 0;
152552217195SDaniel Vetter 
152652217195SDaniel Vetter 	if (edid)
152752217195SDaniel Vetter 		size = EDID_LENGTH * (1 + edid->extensions);
152852217195SDaniel Vetter 
1529170178feSKeith Packard 	/* Set the display info, using edid if available, otherwise
1530170178feSKeith Packard 	 * reseting the values to defaults. This duplicates the work
1531170178feSKeith Packard 	 * done in drm_add_edid_modes, but that function is not
1532170178feSKeith Packard 	 * consistently called before this one in all drivers and the
1533170178feSKeith Packard 	 * computation is cheap enough that it seems better to
1534170178feSKeith Packard 	 * duplicate it rather than attempt to ensure some arbitrary
1535170178feSKeith Packard 	 * ordering of calls.
1536170178feSKeith Packard 	 */
1537170178feSKeith Packard 	if (edid)
1538170178feSKeith Packard 		drm_add_display_info(connector, edid);
1539170178feSKeith Packard 	else
1540170178feSKeith Packard 		drm_reset_display_info(connector);
1541170178feSKeith Packard 
154266660d4cSDave Airlie 	drm_object_property_set_value(&connector->base,
154366660d4cSDave Airlie 				      dev->mode_config.non_desktop_property,
154466660d4cSDave Airlie 				      connector->display_info.non_desktop);
154566660d4cSDave Airlie 
154652217195SDaniel Vetter 	ret = drm_property_replace_global_blob(dev,
154752217195SDaniel Vetter 					       &connector->edid_blob_ptr,
154852217195SDaniel Vetter 	                                       size,
154952217195SDaniel Vetter 	                                       edid,
155052217195SDaniel Vetter 	                                       &connector->base,
155152217195SDaniel Vetter 	                                       dev->mode_config.edid_property);
155252217195SDaniel Vetter 	return ret;
155352217195SDaniel Vetter }
1554c555f023SDaniel Vetter EXPORT_SYMBOL(drm_connector_update_edid_property);
155552217195SDaniel Vetter 
155640ee6fbeSManasi Navare /**
155797e14fbeSDaniel Vetter  * drm_connector_set_link_status_property - Set link status property of a connector
155840ee6fbeSManasi Navare  * @connector: drm connector
155940ee6fbeSManasi Navare  * @link_status: new value of link status property (0: Good, 1: Bad)
156040ee6fbeSManasi Navare  *
156140ee6fbeSManasi Navare  * In usual working scenario, this link status property will always be set to
156240ee6fbeSManasi Navare  * "GOOD". If something fails during or after a mode set, the kernel driver
156340ee6fbeSManasi Navare  * may set this link status property to "BAD". The caller then needs to send a
156440ee6fbeSManasi Navare  * hotplug uevent for userspace to re-check the valid modes through
156540ee6fbeSManasi Navare  * GET_CONNECTOR_IOCTL and retry modeset.
156640ee6fbeSManasi Navare  *
156740ee6fbeSManasi Navare  * Note: Drivers cannot rely on userspace to support this property and
156840ee6fbeSManasi Navare  * issue a modeset. As such, they may choose to handle issues (like
156940ee6fbeSManasi Navare  * re-training a link) without userspace's intervention.
157040ee6fbeSManasi Navare  *
157140ee6fbeSManasi Navare  * The reason for adding this property is to handle link training failures, but
157240ee6fbeSManasi Navare  * it is not limited to DP or link training. For example, if we implement
157340ee6fbeSManasi Navare  * asynchronous setcrtc, this property can be used to report any failures in that.
157440ee6fbeSManasi Navare  */
157597e14fbeSDaniel Vetter void drm_connector_set_link_status_property(struct drm_connector *connector,
157640ee6fbeSManasi Navare 					    uint64_t link_status)
157740ee6fbeSManasi Navare {
157840ee6fbeSManasi Navare 	struct drm_device *dev = connector->dev;
157940ee6fbeSManasi Navare 
158040ee6fbeSManasi Navare 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
158140ee6fbeSManasi Navare 	connector->state->link_status = link_status;
158240ee6fbeSManasi Navare 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
158340ee6fbeSManasi Navare }
158497e14fbeSDaniel Vetter EXPORT_SYMBOL(drm_connector_set_link_status_property);
158540ee6fbeSManasi Navare 
15868d70f395SHans de Goede /**
15878d70f395SHans de Goede  * drm_connector_init_panel_orientation_property -
15888d70f395SHans de Goede  *	initialize the connecters panel_orientation property
15898d70f395SHans de Goede  * @connector: connector for which to init the panel-orientation property.
15908d70f395SHans de Goede  * @width: width in pixels of the panel, used for panel quirk detection
15918d70f395SHans de Goede  * @height: height in pixels of the panel, used for panel quirk detection
15928d70f395SHans de Goede  *
15938d70f395SHans de Goede  * This function should only be called for built-in panels, after setting
15948d70f395SHans de Goede  * connector->display_info.panel_orientation first (if known).
15958d70f395SHans de Goede  *
15968d70f395SHans de Goede  * This function will check for platform specific (e.g. DMI based) quirks
15978d70f395SHans de Goede  * overriding display_info.panel_orientation first, then if panel_orientation
15988d70f395SHans de Goede  * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
15998d70f395SHans de Goede  * "panel orientation" property to the connector.
16008d70f395SHans de Goede  *
16018d70f395SHans de Goede  * Returns:
16028d70f395SHans de Goede  * Zero on success, negative errno on failure.
16038d70f395SHans de Goede  */
16048d70f395SHans de Goede int drm_connector_init_panel_orientation_property(
16058d70f395SHans de Goede 	struct drm_connector *connector, int width, int height)
16068d70f395SHans de Goede {
16078d70f395SHans de Goede 	struct drm_device *dev = connector->dev;
16088d70f395SHans de Goede 	struct drm_display_info *info = &connector->display_info;
16098d70f395SHans de Goede 	struct drm_property *prop;
16108d70f395SHans de Goede 	int orientation_quirk;
16118d70f395SHans de Goede 
16128d70f395SHans de Goede 	orientation_quirk = drm_get_panel_orientation_quirk(width, height);
16138d70f395SHans de Goede 	if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
16148d70f395SHans de Goede 		info->panel_orientation = orientation_quirk;
16158d70f395SHans de Goede 
16168d70f395SHans de Goede 	if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
16178d70f395SHans de Goede 		return 0;
16188d70f395SHans de Goede 
16198d70f395SHans de Goede 	prop = dev->mode_config.panel_orientation_property;
16208d70f395SHans de Goede 	if (!prop) {
16218d70f395SHans de Goede 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
16228d70f395SHans de Goede 				"panel orientation",
16238d70f395SHans de Goede 				drm_panel_orientation_enum_list,
16248d70f395SHans de Goede 				ARRAY_SIZE(drm_panel_orientation_enum_list));
16258d70f395SHans de Goede 		if (!prop)
16268d70f395SHans de Goede 			return -ENOMEM;
16278d70f395SHans de Goede 
16288d70f395SHans de Goede 		dev->mode_config.panel_orientation_property = prop;
16298d70f395SHans de Goede 	}
16308d70f395SHans de Goede 
16318d70f395SHans de Goede 	drm_object_attach_property(&connector->base, prop,
16328d70f395SHans de Goede 				   info->panel_orientation);
16338d70f395SHans de Goede 	return 0;
16348d70f395SHans de Goede }
16358d70f395SHans de Goede EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
16368d70f395SHans de Goede 
163797e14fbeSDaniel Vetter int drm_connector_set_obj_prop(struct drm_mode_object *obj,
163852217195SDaniel Vetter 				    struct drm_property *property,
163952217195SDaniel Vetter 				    uint64_t value)
164052217195SDaniel Vetter {
164152217195SDaniel Vetter 	int ret = -EINVAL;
164252217195SDaniel Vetter 	struct drm_connector *connector = obj_to_connector(obj);
164352217195SDaniel Vetter 
164452217195SDaniel Vetter 	/* Do DPMS ourselves */
164552217195SDaniel Vetter 	if (property == connector->dev->mode_config.dpms_property) {
164652217195SDaniel Vetter 		ret = (*connector->funcs->dpms)(connector, (int)value);
164752217195SDaniel Vetter 	} else if (connector->funcs->set_property)
164852217195SDaniel Vetter 		ret = connector->funcs->set_property(connector, property, value);
164952217195SDaniel Vetter 
1650144a7999SDaniel Vetter 	if (!ret)
165152217195SDaniel Vetter 		drm_object_property_set_value(&connector->base, property, value);
165252217195SDaniel Vetter 	return ret;
165352217195SDaniel Vetter }
165452217195SDaniel Vetter 
165597e14fbeSDaniel Vetter int drm_connector_property_set_ioctl(struct drm_device *dev,
165652217195SDaniel Vetter 				     void *data, struct drm_file *file_priv)
165752217195SDaniel Vetter {
165852217195SDaniel Vetter 	struct drm_mode_connector_set_property *conn_set_prop = data;
165952217195SDaniel Vetter 	struct drm_mode_obj_set_property obj_set_prop = {
166052217195SDaniel Vetter 		.value = conn_set_prop->value,
166152217195SDaniel Vetter 		.prop_id = conn_set_prop->prop_id,
166252217195SDaniel Vetter 		.obj_id = conn_set_prop->connector_id,
166352217195SDaniel Vetter 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
166452217195SDaniel Vetter 	};
166552217195SDaniel Vetter 
166652217195SDaniel Vetter 	/* It does all the locking and checking we need */
166752217195SDaniel Vetter 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
166852217195SDaniel Vetter }
166952217195SDaniel Vetter 
167052217195SDaniel Vetter static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
167152217195SDaniel Vetter {
167252217195SDaniel Vetter 	/* For atomic drivers only state objects are synchronously updated and
167352217195SDaniel Vetter 	 * protected by modeset locks, so check those first. */
167452217195SDaniel Vetter 	if (connector->state)
167552217195SDaniel Vetter 		return connector->state->best_encoder;
167652217195SDaniel Vetter 	return connector->encoder;
167752217195SDaniel Vetter }
167852217195SDaniel Vetter 
1679c3ff0cdbSAnkit Nautiyal static bool
1680c3ff0cdbSAnkit Nautiyal drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1681c3ff0cdbSAnkit Nautiyal 			     const struct list_head *export_list,
168252217195SDaniel Vetter 			     const struct drm_file *file_priv)
168352217195SDaniel Vetter {
168452217195SDaniel Vetter 	/*
168552217195SDaniel Vetter 	 * If user-space hasn't configured the driver to expose the stereo 3D
168652217195SDaniel Vetter 	 * modes, don't expose them.
168752217195SDaniel Vetter 	 */
168852217195SDaniel Vetter 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
168952217195SDaniel Vetter 		return false;
1690c3ff0cdbSAnkit Nautiyal 	/*
1691c3ff0cdbSAnkit Nautiyal 	 * If user-space hasn't configured the driver to expose the modes
1692c3ff0cdbSAnkit Nautiyal 	 * with aspect-ratio, don't expose them. However if such a mode
1693c3ff0cdbSAnkit Nautiyal 	 * is unique, let it be exposed, but reset the aspect-ratio flags
1694c3ff0cdbSAnkit Nautiyal 	 * while preparing the list of user-modes.
1695c3ff0cdbSAnkit Nautiyal 	 */
1696c3ff0cdbSAnkit Nautiyal 	if (!file_priv->aspect_ratio_allowed) {
1697c3ff0cdbSAnkit Nautiyal 		struct drm_display_mode *mode_itr;
1698c3ff0cdbSAnkit Nautiyal 
1699c3ff0cdbSAnkit Nautiyal 		list_for_each_entry(mode_itr, export_list, export_head)
1700c3ff0cdbSAnkit Nautiyal 			if (drm_mode_match(mode_itr, mode,
1701c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_TIMINGS |
1702c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_CLOCK |
1703c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_FLAGS |
1704c3ff0cdbSAnkit Nautiyal 					   DRM_MODE_MATCH_3D_FLAGS))
1705c3ff0cdbSAnkit Nautiyal 				return false;
1706c3ff0cdbSAnkit Nautiyal 	}
170752217195SDaniel Vetter 
170852217195SDaniel Vetter 	return true;
170952217195SDaniel Vetter }
171052217195SDaniel Vetter 
171152217195SDaniel Vetter int drm_mode_getconnector(struct drm_device *dev, void *data,
171252217195SDaniel Vetter 			  struct drm_file *file_priv)
171352217195SDaniel Vetter {
171452217195SDaniel Vetter 	struct drm_mode_get_connector *out_resp = data;
171552217195SDaniel Vetter 	struct drm_connector *connector;
171652217195SDaniel Vetter 	struct drm_encoder *encoder;
171752217195SDaniel Vetter 	struct drm_display_mode *mode;
171852217195SDaniel Vetter 	int mode_count = 0;
171952217195SDaniel Vetter 	int encoders_count = 0;
172052217195SDaniel Vetter 	int ret = 0;
172152217195SDaniel Vetter 	int copied = 0;
172252217195SDaniel Vetter 	int i;
172352217195SDaniel Vetter 	struct drm_mode_modeinfo u_mode;
172452217195SDaniel Vetter 	struct drm_mode_modeinfo __user *mode_ptr;
172552217195SDaniel Vetter 	uint32_t __user *encoder_ptr;
1726c3ff0cdbSAnkit Nautiyal 	LIST_HEAD(export_list);
172752217195SDaniel Vetter 
172852217195SDaniel Vetter 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
172969fdf420SChris Wilson 		return -EOPNOTSUPP;
173052217195SDaniel Vetter 
173152217195SDaniel Vetter 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
173252217195SDaniel Vetter 
1733418da172SKeith Packard 	connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
173491eefc05SDaniel Vetter 	if (!connector)
173591eefc05SDaniel Vetter 		return -ENOENT;
173652217195SDaniel Vetter 
173783aefbb8SVille Syrjälä 	drm_connector_for_each_possible_encoder(connector, encoder, i)
173891eefc05SDaniel Vetter 		encoders_count++;
173991eefc05SDaniel Vetter 
174091eefc05SDaniel Vetter 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
174191eefc05SDaniel Vetter 		copied = 0;
174291eefc05SDaniel Vetter 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
174383aefbb8SVille Syrjälä 
174483aefbb8SVille Syrjälä 		drm_connector_for_each_possible_encoder(connector, encoder, i) {
174583aefbb8SVille Syrjälä 			if (put_user(encoder->base.id, encoder_ptr + copied)) {
174691eefc05SDaniel Vetter 				ret = -EFAULT;
1747e94ac351SDaniel Vetter 				goto out;
174891eefc05SDaniel Vetter 			}
174991eefc05SDaniel Vetter 			copied++;
175091eefc05SDaniel Vetter 		}
175191eefc05SDaniel Vetter 	}
175291eefc05SDaniel Vetter 	out_resp->count_encoders = encoders_count;
175391eefc05SDaniel Vetter 
175491eefc05SDaniel Vetter 	out_resp->connector_id = connector->base.id;
175591eefc05SDaniel Vetter 	out_resp->connector_type = connector->connector_type;
175691eefc05SDaniel Vetter 	out_resp->connector_type_id = connector->connector_type_id;
175791eefc05SDaniel Vetter 
175891eefc05SDaniel Vetter 	mutex_lock(&dev->mode_config.mutex);
175991eefc05SDaniel Vetter 	if (out_resp->count_modes == 0) {
176091eefc05SDaniel Vetter 		connector->funcs->fill_modes(connector,
176191eefc05SDaniel Vetter 					     dev->mode_config.max_width,
176291eefc05SDaniel Vetter 					     dev->mode_config.max_height);
176391eefc05SDaniel Vetter 	}
176491eefc05SDaniel Vetter 
176591eefc05SDaniel Vetter 	out_resp->mm_width = connector->display_info.width_mm;
176691eefc05SDaniel Vetter 	out_resp->mm_height = connector->display_info.height_mm;
176791eefc05SDaniel Vetter 	out_resp->subpixel = connector->display_info.subpixel_order;
176891eefc05SDaniel Vetter 	out_resp->connection = connector->status;
176991eefc05SDaniel Vetter 
177091eefc05SDaniel Vetter 	/* delayed so we get modes regardless of pre-fill_modes state */
177191eefc05SDaniel Vetter 	list_for_each_entry(mode, &connector->modes, head)
1772c3ff0cdbSAnkit Nautiyal 		if (drm_mode_expose_to_userspace(mode, &export_list,
1773c3ff0cdbSAnkit Nautiyal 						 file_priv)) {
1774c3ff0cdbSAnkit Nautiyal 			list_add_tail(&mode->export_head, &export_list);
177591eefc05SDaniel Vetter 			mode_count++;
1776c3ff0cdbSAnkit Nautiyal 		}
177791eefc05SDaniel Vetter 
177852217195SDaniel Vetter 	/*
177952217195SDaniel Vetter 	 * This ioctl is called twice, once to determine how much space is
178052217195SDaniel Vetter 	 * needed, and the 2nd time to fill it.
1781c3ff0cdbSAnkit Nautiyal 	 * The modes that need to be exposed to the user are maintained in the
1782c3ff0cdbSAnkit Nautiyal 	 * 'export_list'. When the ioctl is called first time to determine the,
1783c3ff0cdbSAnkit Nautiyal 	 * space, the export_list gets filled, to find the no.of modes. In the
1784c3ff0cdbSAnkit Nautiyal 	 * 2nd time, the user modes are filled, one by one from the export_list.
178552217195SDaniel Vetter 	 */
178652217195SDaniel Vetter 	if ((out_resp->count_modes >= mode_count) && mode_count) {
178752217195SDaniel Vetter 		copied = 0;
178852217195SDaniel Vetter 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1789c3ff0cdbSAnkit Nautiyal 		list_for_each_entry(mode, &export_list, export_head) {
179052217195SDaniel Vetter 			drm_mode_convert_to_umode(&u_mode, mode);
1791c3ff0cdbSAnkit Nautiyal 			/*
1792c3ff0cdbSAnkit Nautiyal 			 * Reset aspect ratio flags of user-mode, if modes with
1793c3ff0cdbSAnkit Nautiyal 			 * aspect-ratio are not supported.
1794c3ff0cdbSAnkit Nautiyal 			 */
1795c3ff0cdbSAnkit Nautiyal 			if (!file_priv->aspect_ratio_allowed)
1796c3ff0cdbSAnkit Nautiyal 				u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
179752217195SDaniel Vetter 			if (copy_to_user(mode_ptr + copied,
179852217195SDaniel Vetter 					 &u_mode, sizeof(u_mode))) {
179952217195SDaniel Vetter 				ret = -EFAULT;
1800e94ac351SDaniel Vetter 				mutex_unlock(&dev->mode_config.mutex);
1801e94ac351SDaniel Vetter 
180252217195SDaniel Vetter 				goto out;
180352217195SDaniel Vetter 			}
180452217195SDaniel Vetter 			copied++;
180552217195SDaniel Vetter 		}
180652217195SDaniel Vetter 	}
180752217195SDaniel Vetter 	out_resp->count_modes = mode_count;
180852217195SDaniel Vetter 	mutex_unlock(&dev->mode_config.mutex);
1809e94ac351SDaniel Vetter 
1810e94ac351SDaniel Vetter 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1811e94ac351SDaniel Vetter 	encoder = drm_connector_get_encoder(connector);
1812e94ac351SDaniel Vetter 	if (encoder)
1813e94ac351SDaniel Vetter 		out_resp->encoder_id = encoder->base.id;
1814e94ac351SDaniel Vetter 	else
1815e94ac351SDaniel Vetter 		out_resp->encoder_id = 0;
1816e94ac351SDaniel Vetter 
1817e94ac351SDaniel Vetter 	/* Only grab properties after probing, to make sure EDID and other
1818e94ac351SDaniel Vetter 	 * properties reflect the latest status. */
1819e94ac351SDaniel Vetter 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1820e94ac351SDaniel Vetter 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1821e94ac351SDaniel Vetter 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1822e94ac351SDaniel Vetter 			&out_resp->count_props);
1823e94ac351SDaniel Vetter 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1824e94ac351SDaniel Vetter 
1825e94ac351SDaniel Vetter out:
1826ad093607SThierry Reding 	drm_connector_put(connector);
182752217195SDaniel Vetter 
182852217195SDaniel Vetter 	return ret;
182952217195SDaniel Vetter }
183052217195SDaniel Vetter 
18319498c19bSDaniel Vetter 
18329498c19bSDaniel Vetter /**
18339498c19bSDaniel Vetter  * DOC: Tile group
18349498c19bSDaniel Vetter  *
18359498c19bSDaniel Vetter  * Tile groups are used to represent tiled monitors with a unique integer
18369498c19bSDaniel Vetter  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
18379498c19bSDaniel Vetter  * we store this in a tile group, so we have a common identifier for all tiles
18389498c19bSDaniel Vetter  * in a monitor group. The property is called "TILE". Drivers can manage tile
18399498c19bSDaniel Vetter  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
18409498c19bSDaniel Vetter  * drm_mode_get_tile_group(). But this is only needed for internal panels where
18419498c19bSDaniel Vetter  * the tile group information is exposed through a non-standard way.
18429498c19bSDaniel Vetter  */
18439498c19bSDaniel Vetter 
18449498c19bSDaniel Vetter static void drm_tile_group_free(struct kref *kref)
18459498c19bSDaniel Vetter {
18469498c19bSDaniel Vetter 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
18479498c19bSDaniel Vetter 	struct drm_device *dev = tg->dev;
18489498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
18499498c19bSDaniel Vetter 	idr_remove(&dev->mode_config.tile_idr, tg->id);
18509498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
18519498c19bSDaniel Vetter 	kfree(tg);
18529498c19bSDaniel Vetter }
18539498c19bSDaniel Vetter 
18549498c19bSDaniel Vetter /**
18559498c19bSDaniel Vetter  * drm_mode_put_tile_group - drop a reference to a tile group.
18569498c19bSDaniel Vetter  * @dev: DRM device
18579498c19bSDaniel Vetter  * @tg: tile group to drop reference to.
18589498c19bSDaniel Vetter  *
18599498c19bSDaniel Vetter  * drop reference to tile group and free if 0.
18609498c19bSDaniel Vetter  */
18619498c19bSDaniel Vetter void drm_mode_put_tile_group(struct drm_device *dev,
18629498c19bSDaniel Vetter 			     struct drm_tile_group *tg)
18639498c19bSDaniel Vetter {
18649498c19bSDaniel Vetter 	kref_put(&tg->refcount, drm_tile_group_free);
18659498c19bSDaniel Vetter }
18669498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_put_tile_group);
18679498c19bSDaniel Vetter 
18689498c19bSDaniel Vetter /**
18699498c19bSDaniel Vetter  * drm_mode_get_tile_group - get a reference to an existing tile group
18709498c19bSDaniel Vetter  * @dev: DRM device
18719498c19bSDaniel Vetter  * @topology: 8-bytes unique per monitor.
18729498c19bSDaniel Vetter  *
18739498c19bSDaniel Vetter  * Use the unique bytes to get a reference to an existing tile group.
18749498c19bSDaniel Vetter  *
18759498c19bSDaniel Vetter  * RETURNS:
18769498c19bSDaniel Vetter  * tile group or NULL if not found.
18779498c19bSDaniel Vetter  */
18789498c19bSDaniel Vetter struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
18799498c19bSDaniel Vetter 					       char topology[8])
18809498c19bSDaniel Vetter {
18819498c19bSDaniel Vetter 	struct drm_tile_group *tg;
18829498c19bSDaniel Vetter 	int id;
18839498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
18849498c19bSDaniel Vetter 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
18859498c19bSDaniel Vetter 		if (!memcmp(tg->group_data, topology, 8)) {
18869498c19bSDaniel Vetter 			if (!kref_get_unless_zero(&tg->refcount))
18879498c19bSDaniel Vetter 				tg = NULL;
18889498c19bSDaniel Vetter 			mutex_unlock(&dev->mode_config.idr_mutex);
18899498c19bSDaniel Vetter 			return tg;
18909498c19bSDaniel Vetter 		}
18919498c19bSDaniel Vetter 	}
18929498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
18939498c19bSDaniel Vetter 	return NULL;
18949498c19bSDaniel Vetter }
18959498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_get_tile_group);
18969498c19bSDaniel Vetter 
18979498c19bSDaniel Vetter /**
18989498c19bSDaniel Vetter  * drm_mode_create_tile_group - create a tile group from a displayid description
18999498c19bSDaniel Vetter  * @dev: DRM device
19009498c19bSDaniel Vetter  * @topology: 8-bytes unique per monitor.
19019498c19bSDaniel Vetter  *
19029498c19bSDaniel Vetter  * Create a tile group for the unique monitor, and get a unique
19039498c19bSDaniel Vetter  * identifier for the tile group.
19049498c19bSDaniel Vetter  *
19059498c19bSDaniel Vetter  * RETURNS:
19069498c19bSDaniel Vetter  * new tile group or error.
19079498c19bSDaniel Vetter  */
19089498c19bSDaniel Vetter struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
19099498c19bSDaniel Vetter 						  char topology[8])
19109498c19bSDaniel Vetter {
19119498c19bSDaniel Vetter 	struct drm_tile_group *tg;
19129498c19bSDaniel Vetter 	int ret;
19139498c19bSDaniel Vetter 
19149498c19bSDaniel Vetter 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
19159498c19bSDaniel Vetter 	if (!tg)
19169498c19bSDaniel Vetter 		return ERR_PTR(-ENOMEM);
19179498c19bSDaniel Vetter 
19189498c19bSDaniel Vetter 	kref_init(&tg->refcount);
19199498c19bSDaniel Vetter 	memcpy(tg->group_data, topology, 8);
19209498c19bSDaniel Vetter 	tg->dev = dev;
19219498c19bSDaniel Vetter 
19229498c19bSDaniel Vetter 	mutex_lock(&dev->mode_config.idr_mutex);
19239498c19bSDaniel Vetter 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
19249498c19bSDaniel Vetter 	if (ret >= 0) {
19259498c19bSDaniel Vetter 		tg->id = ret;
19269498c19bSDaniel Vetter 	} else {
19279498c19bSDaniel Vetter 		kfree(tg);
19289498c19bSDaniel Vetter 		tg = ERR_PTR(ret);
19299498c19bSDaniel Vetter 	}
19309498c19bSDaniel Vetter 
19319498c19bSDaniel Vetter 	mutex_unlock(&dev->mode_config.idr_mutex);
19329498c19bSDaniel Vetter 	return tg;
19339498c19bSDaniel Vetter }
19349498c19bSDaniel Vetter EXPORT_SYMBOL(drm_mode_create_tile_group);
1935