xref: /linux/drivers/gpu/drm/drm_connector.c (revision c156ef573efe4230ef3dc1ff2ec0038fe0eb217f)
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <drm/drm_auth.h>
24 #include <drm/drm_connector.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_edid.h>
27 #include <drm/drm_encoder.h>
28 #include <drm/drm_file.h>
29 #include <drm/drm_managed.h>
30 #include <drm/drm_panel.h>
31 #include <drm/drm_print.h>
32 #include <drm/drm_privacy_screen_consumer.h>
33 #include <drm/drm_sysfs.h>
34 #include <drm/drm_utils.h>
35 
36 #include <linux/property.h>
37 #include <linux/uaccess.h>
38 
39 #include <video/cmdline.h>
40 
41 #include "drm_crtc_internal.h"
42 #include "drm_internal.h"
43 
44 /**
45  * DOC: overview
46  *
47  * In DRM connectors are the general abstraction for display sinks, and include
48  * also fixed panels or anything else that can display pixels in some form. As
49  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
50  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
51  * Hence they are reference-counted using drm_connector_get() and
52  * drm_connector_put().
53  *
54  * KMS driver must create, initialize, register and attach at a &struct
55  * drm_connector for each such sink. The instance is created as other KMS
56  * objects and initialized by setting the following fields. The connector is
57  * initialized with a call to drm_connector_init() with a pointer to the
58  * &struct drm_connector_funcs and a connector type, and then exposed to
59  * userspace with a call to drm_connector_register().
60  *
61  * Connectors must be attached to an encoder to be used. For devices that map
62  * connectors to encoders 1:1, the connector should be attached at
63  * initialization time with a call to drm_connector_attach_encoder(). The
64  * driver must also set the &drm_connector.encoder field to point to the
65  * attached encoder.
66  *
67  * For connectors which are not fixed (like built-in panels) the driver needs to
68  * support hotplug notifications. The simplest way to do that is by using the
69  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
70  * hardware support for hotplug interrupts. Connectors with hardware hotplug
71  * support can instead use e.g. drm_helper_hpd_irq_event().
72  */
73 
74 /*
75  * Global connector list for drm_connector_find_by_fwnode().
76  * Note drm_connector_[un]register() first take connector->lock and then
77  * take the connector_list_lock.
78  */
79 static DEFINE_MUTEX(connector_list_lock);
80 static LIST_HEAD(connector_list);
81 
82 struct drm_conn_prop_enum_list {
83 	int type;
84 	const char *name;
85 	struct ida ida;
86 };
87 
88 /*
89  * Connector and encoder types.
90  */
91 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
92 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
93 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
94 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
95 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
96 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
97 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
98 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
99 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
100 	{ DRM_MODE_CONNECTOR_Component, "Component" },
101 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
102 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
103 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
104 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
105 	{ DRM_MODE_CONNECTOR_TV, "TV" },
106 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
107 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
108 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
109 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
110 	{ DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
111 	{ DRM_MODE_CONNECTOR_SPI, "SPI" },
112 	{ DRM_MODE_CONNECTOR_USB, "USB" },
113 };
114 
115 void drm_connector_ida_init(void)
116 {
117 	int i;
118 
119 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
120 		ida_init(&drm_connector_enum_list[i].ida);
121 }
122 
123 void drm_connector_ida_destroy(void)
124 {
125 	int i;
126 
127 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
128 		ida_destroy(&drm_connector_enum_list[i].ida);
129 }
130 
131 /**
132  * drm_get_connector_type_name - return a string for connector type
133  * @type: The connector type (DRM_MODE_CONNECTOR_*)
134  *
135  * Returns: the name of the connector type, or NULL if the type is not valid.
136  */
137 const char *drm_get_connector_type_name(unsigned int type)
138 {
139 	if (type < ARRAY_SIZE(drm_connector_enum_list))
140 		return drm_connector_enum_list[type].name;
141 
142 	return NULL;
143 }
144 EXPORT_SYMBOL(drm_get_connector_type_name);
145 
146 /**
147  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
148  * @connector: connector to query
149  *
150  * The kernel supports per-connector configuration of its consoles through
151  * use of the video= parameter. This function parses that option and
152  * extracts the user's specified mode (or enable/disable status) for a
153  * particular connector. This is typically only used during the early fbdev
154  * setup.
155  */
156 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
157 {
158 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
159 	const char *option;
160 
161 	option = video_get_options(connector->name);
162 	if (!option)
163 		return;
164 
165 	if (!drm_mode_parse_command_line_for_connector(option,
166 						       connector,
167 						       mode))
168 		return;
169 
170 	if (mode->force) {
171 		DRM_INFO("forcing %s connector %s\n", connector->name,
172 			 drm_get_connector_force_name(mode->force));
173 		connector->force = mode->force;
174 	}
175 
176 	if (mode->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN) {
177 		DRM_INFO("cmdline forces connector %s panel_orientation to %d\n",
178 			 connector->name, mode->panel_orientation);
179 		drm_connector_set_panel_orientation(connector,
180 						    mode->panel_orientation);
181 	}
182 
183 	DRM_DEBUG_KMS("cmdline mode for connector %s %s %dx%d@%dHz%s%s%s\n",
184 		      connector->name, mode->name,
185 		      mode->xres, mode->yres,
186 		      mode->refresh_specified ? mode->refresh : 60,
187 		      mode->rb ? " reduced blanking" : "",
188 		      mode->margins ? " with margins" : "",
189 		      mode->interlace ?  " interlaced" : "");
190 }
191 
192 static void drm_connector_free(struct kref *kref)
193 {
194 	struct drm_connector *connector =
195 		container_of(kref, struct drm_connector, base.refcount);
196 	struct drm_device *dev = connector->dev;
197 
198 	drm_mode_object_unregister(dev, &connector->base);
199 	connector->funcs->destroy(connector);
200 }
201 
202 void drm_connector_free_work_fn(struct work_struct *work)
203 {
204 	struct drm_connector *connector, *n;
205 	struct drm_device *dev =
206 		container_of(work, struct drm_device, mode_config.connector_free_work);
207 	struct drm_mode_config *config = &dev->mode_config;
208 	unsigned long flags;
209 	struct llist_node *freed;
210 
211 	spin_lock_irqsave(&config->connector_list_lock, flags);
212 	freed = llist_del_all(&config->connector_free_list);
213 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
214 
215 	llist_for_each_entry_safe(connector, n, freed, free_node) {
216 		drm_mode_object_unregister(dev, &connector->base);
217 		connector->funcs->destroy(connector);
218 	}
219 }
220 
221 static int drm_connector_init_only(struct drm_device *dev,
222 				   struct drm_connector *connector,
223 				   const struct drm_connector_funcs *funcs,
224 				   int connector_type,
225 				   struct i2c_adapter *ddc)
226 {
227 	struct drm_mode_config *config = &dev->mode_config;
228 	int ret;
229 	struct ida *connector_ida =
230 		&drm_connector_enum_list[connector_type].ida;
231 
232 	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
233 		(!funcs->atomic_destroy_state ||
234 		 !funcs->atomic_duplicate_state));
235 
236 	ret = __drm_mode_object_add(dev, &connector->base,
237 				    DRM_MODE_OBJECT_CONNECTOR,
238 				    false, drm_connector_free);
239 	if (ret)
240 		return ret;
241 
242 	connector->base.properties = &connector->properties;
243 	connector->dev = dev;
244 	connector->funcs = funcs;
245 
246 	/* connector index is used with 32bit bitmasks */
247 	ret = ida_alloc_max(&config->connector_ida, 31, GFP_KERNEL);
248 	if (ret < 0) {
249 		DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
250 			      drm_connector_enum_list[connector_type].name,
251 			      ret);
252 		goto out_put;
253 	}
254 	connector->index = ret;
255 	ret = 0;
256 
257 	connector->connector_type = connector_type;
258 	connector->connector_type_id =
259 		ida_alloc_min(connector_ida, 1, GFP_KERNEL);
260 	if (connector->connector_type_id < 0) {
261 		ret = connector->connector_type_id;
262 		goto out_put_id;
263 	}
264 	connector->name =
265 		kasprintf(GFP_KERNEL, "%s-%d",
266 			  drm_connector_enum_list[connector_type].name,
267 			  connector->connector_type_id);
268 	if (!connector->name) {
269 		ret = -ENOMEM;
270 		goto out_put_type_id;
271 	}
272 
273 	/* provide ddc symlink in sysfs */
274 	connector->ddc = ddc;
275 
276 	INIT_LIST_HEAD(&connector->head);
277 	INIT_LIST_HEAD(&connector->global_connector_list_entry);
278 	INIT_LIST_HEAD(&connector->probed_modes);
279 	INIT_LIST_HEAD(&connector->modes);
280 	mutex_init(&connector->mutex);
281 	mutex_init(&connector->eld_mutex);
282 	mutex_init(&connector->edid_override_mutex);
283 	mutex_init(&connector->hdmi.infoframes.lock);
284 	connector->edid_blob_ptr = NULL;
285 	connector->epoch_counter = 0;
286 	connector->tile_blob_ptr = NULL;
287 	connector->status = connector_status_unknown;
288 	connector->display_info.panel_orientation =
289 		DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
290 
291 	drm_connector_get_cmdline_mode(connector);
292 
293 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
294 	    connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
295 		drm_connector_attach_edid_property(connector);
296 
297 	drm_object_attach_property(&connector->base,
298 				      config->dpms_property, 0);
299 
300 	drm_object_attach_property(&connector->base,
301 				   config->link_status_property,
302 				   0);
303 
304 	drm_object_attach_property(&connector->base,
305 				   config->non_desktop_property,
306 				   0);
307 	drm_object_attach_property(&connector->base,
308 				   config->tile_property,
309 				   0);
310 
311 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
312 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
313 	}
314 
315 	connector->debugfs_entry = NULL;
316 out_put_type_id:
317 	if (ret)
318 		ida_free(connector_ida, connector->connector_type_id);
319 out_put_id:
320 	if (ret)
321 		ida_free(&config->connector_ida, connector->index);
322 out_put:
323 	if (ret)
324 		drm_mode_object_unregister(dev, &connector->base);
325 
326 	return ret;
327 }
328 
329 static void drm_connector_add(struct drm_connector *connector)
330 {
331 	struct drm_device *dev = connector->dev;
332 	struct drm_mode_config *config = &dev->mode_config;
333 
334 	if (drm_WARN_ON(dev, !list_empty(&connector->head)))
335 		return;
336 
337 	spin_lock_irq(&config->connector_list_lock);
338 	list_add_tail(&connector->head, &config->connector_list);
339 	config->num_connector++;
340 	spin_unlock_irq(&config->connector_list_lock);
341 }
342 
343 static void drm_connector_remove(struct drm_connector *connector)
344 {
345 	struct drm_device *dev = connector->dev;
346 
347 	/*
348 	 * For dynamic connectors drm_connector_cleanup() can call this function
349 	 * before the connector is registered and added to the list.
350 	 */
351 	if (list_empty(&connector->head))
352 		return;
353 
354 	spin_lock_irq(&dev->mode_config.connector_list_lock);
355 	list_del_init(&connector->head);
356 	dev->mode_config.num_connector--;
357 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
358 }
359 
360 static int drm_connector_init_and_add(struct drm_device *dev,
361 				      struct drm_connector *connector,
362 				      const struct drm_connector_funcs *funcs,
363 				      int connector_type,
364 				      struct i2c_adapter *ddc)
365 {
366 	int ret;
367 
368 	ret = drm_connector_init_only(dev, connector, funcs, connector_type, ddc);
369 	if (ret)
370 		return ret;
371 
372 	drm_connector_add(connector);
373 
374 	return 0;
375 }
376 
377 /**
378  * drm_connector_init - Init a preallocated connector
379  * @dev: DRM device
380  * @connector: the connector to init
381  * @funcs: callbacks for this connector
382  * @connector_type: user visible type of the connector
383  *
384  * Initialises a preallocated connector. Connectors should be
385  * subclassed as part of driver connector objects.
386  *
387  * At driver unload time the driver's &drm_connector_funcs.destroy hook
388  * should call drm_connector_cleanup() and free the connector structure.
389  * The connector structure should not be allocated with devm_kzalloc().
390  *
391  * Note: consider using drmm_connector_init() instead of
392  * drm_connector_init() to let the DRM managed resource infrastructure
393  * take care of cleanup and deallocation.
394  *
395  * Returns:
396  * Zero on success, error code on failure.
397  */
398 int drm_connector_init(struct drm_device *dev,
399 		       struct drm_connector *connector,
400 		       const struct drm_connector_funcs *funcs,
401 		       int connector_type)
402 {
403 	if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
404 		return -EINVAL;
405 
406 	return drm_connector_init_and_add(dev, connector, funcs, connector_type, NULL);
407 }
408 EXPORT_SYMBOL(drm_connector_init);
409 
410 /**
411  * drm_connector_dynamic_init - Init a preallocated dynamic connector
412  * @dev: DRM device
413  * @connector: the connector to init
414  * @funcs: callbacks for this connector
415  * @connector_type: user visible type of the connector
416  * @ddc: pointer to the associated ddc adapter
417  *
418  * Initialises a preallocated dynamic connector. Connectors should be
419  * subclassed as part of driver connector objects. The connector
420  * structure should not be allocated with devm_kzalloc().
421  *
422  * Drivers should call this for dynamic connectors which can be hotplugged
423  * after drm_dev_register() has been called already, e.g. DP MST connectors.
424  * For all other - static - connectors, drivers should call one of the
425  * drm_connector_init*()/drmm_connector_init*() functions.
426  *
427  * After calling this function the drivers must call
428  * drm_connector_dynamic_register().
429  *
430  * To remove the connector the driver must call drm_connector_unregister()
431  * followed by drm_connector_put(). Putting the last reference will call the
432  * driver's &drm_connector_funcs.destroy hook, which in turn must call
433  * drm_connector_cleanup() and free the connector structure.
434  *
435  * Returns:
436  * Zero on success, error code on failure.
437  */
438 int drm_connector_dynamic_init(struct drm_device *dev,
439 			       struct drm_connector *connector,
440 			       const struct drm_connector_funcs *funcs,
441 			       int connector_type,
442 			       struct i2c_adapter *ddc)
443 {
444 	if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
445 		return -EINVAL;
446 
447 	return drm_connector_init_only(dev, connector, funcs, connector_type, ddc);
448 }
449 EXPORT_SYMBOL(drm_connector_dynamic_init);
450 
451 /**
452  * drm_connector_init_with_ddc - Init a preallocated connector
453  * @dev: DRM device
454  * @connector: the connector to init
455  * @funcs: callbacks for this connector
456  * @connector_type: user visible type of the connector
457  * @ddc: pointer to the associated ddc adapter
458  *
459  * Initialises a preallocated connector. Connectors should be
460  * subclassed as part of driver connector objects.
461  *
462  * At driver unload time the driver's &drm_connector_funcs.destroy hook
463  * should call drm_connector_cleanup() and free the connector structure.
464  * The connector structure should not be allocated with devm_kzalloc().
465  *
466  * Ensures that the ddc field of the connector is correctly set.
467  *
468  * Note: consider using drmm_connector_init() instead of
469  * drm_connector_init_with_ddc() to let the DRM managed resource
470  * infrastructure take care of cleanup and deallocation.
471  *
472  * Returns:
473  * Zero on success, error code on failure.
474  */
475 int drm_connector_init_with_ddc(struct drm_device *dev,
476 				struct drm_connector *connector,
477 				const struct drm_connector_funcs *funcs,
478 				int connector_type,
479 				struct i2c_adapter *ddc)
480 {
481 	if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
482 		return -EINVAL;
483 
484 	return drm_connector_init_and_add(dev, connector, funcs, connector_type, ddc);
485 }
486 EXPORT_SYMBOL(drm_connector_init_with_ddc);
487 
488 static void drm_connector_cleanup_action(struct drm_device *dev,
489 					 void *ptr)
490 {
491 	struct drm_connector *connector = ptr;
492 
493 	drm_connector_cleanup(connector);
494 }
495 
496 /**
497  * drmm_connector_init - Init a preallocated connector
498  * @dev: DRM device
499  * @connector: the connector to init
500  * @funcs: callbacks for this connector
501  * @connector_type: user visible type of the connector
502  * @ddc: optional pointer to the associated ddc adapter
503  *
504  * Initialises a preallocated connector. Connectors should be
505  * subclassed as part of driver connector objects.
506  *
507  * Cleanup is automatically handled with a call to
508  * drm_connector_cleanup() in a DRM-managed action.
509  *
510  * The connector structure should be allocated with drmm_kzalloc().
511  *
512  * The @drm_connector_funcs.destroy hook must be NULL.
513  *
514  * Returns:
515  * Zero on success, error code on failure.
516  */
517 int drmm_connector_init(struct drm_device *dev,
518 			struct drm_connector *connector,
519 			const struct drm_connector_funcs *funcs,
520 			int connector_type,
521 			struct i2c_adapter *ddc)
522 {
523 	int ret;
524 
525 	if (drm_WARN_ON(dev, funcs && funcs->destroy))
526 		return -EINVAL;
527 
528 	ret = drm_connector_init_and_add(dev, connector, funcs, connector_type, ddc);
529 	if (ret)
530 		return ret;
531 
532 	ret = drmm_add_action_or_reset(dev, drm_connector_cleanup_action,
533 				       connector);
534 	if (ret)
535 		return ret;
536 
537 	return 0;
538 }
539 EXPORT_SYMBOL(drmm_connector_init);
540 
541 /**
542  * drmm_connector_hdmi_init - Init a preallocated HDMI connector
543  * @dev: DRM device
544  * @connector: A pointer to the HDMI connector to init
545  * @vendor: HDMI Controller Vendor name
546  * @product: HDMI Controller Product name
547  * @funcs: callbacks for this connector
548  * @hdmi_funcs: HDMI-related callbacks for this connector
549  * @connector_type: user visible type of the connector
550  * @ddc: optional pointer to the associated ddc adapter
551  * @supported_formats: Bitmask of @hdmi_colorspace listing supported output formats
552  * @max_bpc: Maximum bits per char the HDMI connector supports
553  *
554  * Initialises a preallocated HDMI connector. Connectors can be
555  * subclassed as part of driver connector objects.
556  *
557  * Cleanup is automatically handled with a call to
558  * drm_connector_cleanup() in a DRM-managed action.
559  *
560  * The connector structure should be allocated with drmm_kzalloc().
561  *
562  * The @drm_connector_funcs.destroy hook must be NULL.
563  *
564  * Returns:
565  * Zero on success, error code on failure.
566  */
567 int drmm_connector_hdmi_init(struct drm_device *dev,
568 			     struct drm_connector *connector,
569 			     const char *vendor, const char *product,
570 			     const struct drm_connector_funcs *funcs,
571 			     const struct drm_connector_hdmi_funcs *hdmi_funcs,
572 			     int connector_type,
573 			     struct i2c_adapter *ddc,
574 			     unsigned long supported_formats,
575 			     unsigned int max_bpc)
576 {
577 	int ret;
578 
579 	if (!vendor || !product)
580 		return -EINVAL;
581 
582 	if ((strlen(vendor) > DRM_CONNECTOR_HDMI_VENDOR_LEN) ||
583 	    (strlen(product) > DRM_CONNECTOR_HDMI_PRODUCT_LEN))
584 		return -EINVAL;
585 
586 	if (!(connector_type == DRM_MODE_CONNECTOR_HDMIA ||
587 	      connector_type == DRM_MODE_CONNECTOR_HDMIB))
588 		return -EINVAL;
589 
590 	if (!supported_formats || !(supported_formats & BIT(HDMI_COLORSPACE_RGB)))
591 		return -EINVAL;
592 
593 	if (!(max_bpc == 8 || max_bpc == 10 || max_bpc == 12))
594 		return -EINVAL;
595 
596 	ret = drmm_connector_init(dev, connector, funcs, connector_type, ddc);
597 	if (ret)
598 		return ret;
599 
600 	connector->hdmi.supported_formats = supported_formats;
601 	strtomem_pad(connector->hdmi.vendor, vendor, 0);
602 	strtomem_pad(connector->hdmi.product, product, 0);
603 
604 	/*
605 	 * drm_connector_attach_max_bpc_property() requires the
606 	 * connector to have a state.
607 	 */
608 	if (connector->funcs->reset)
609 		connector->funcs->reset(connector);
610 
611 	drm_connector_attach_max_bpc_property(connector, 8, max_bpc);
612 	connector->max_bpc = max_bpc;
613 
614 	if (max_bpc > 8)
615 		drm_connector_attach_hdr_output_metadata_property(connector);
616 
617 	connector->hdmi.funcs = hdmi_funcs;
618 
619 	return 0;
620 }
621 EXPORT_SYMBOL(drmm_connector_hdmi_init);
622 
623 /**
624  * drm_connector_attach_edid_property - attach edid property.
625  * @connector: the connector
626  *
627  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
628  * edid property attached by default.  This function can be used to
629  * explicitly enable the edid property in these cases.
630  */
631 void drm_connector_attach_edid_property(struct drm_connector *connector)
632 {
633 	struct drm_mode_config *config = &connector->dev->mode_config;
634 
635 	drm_object_attach_property(&connector->base,
636 				   config->edid_property,
637 				   0);
638 }
639 EXPORT_SYMBOL(drm_connector_attach_edid_property);
640 
641 /**
642  * drm_connector_attach_encoder - attach a connector to an encoder
643  * @connector: connector to attach
644  * @encoder: encoder to attach @connector to
645  *
646  * This function links up a connector to an encoder. Note that the routing
647  * restrictions between encoders and crtcs are exposed to userspace through the
648  * possible_clones and possible_crtcs bitmasks.
649  *
650  * Returns:
651  * Zero on success, negative errno on failure.
652  */
653 int drm_connector_attach_encoder(struct drm_connector *connector,
654 				 struct drm_encoder *encoder)
655 {
656 	/*
657 	 * In the past, drivers have attempted to model the static association
658 	 * of connector to encoder in simple connector/encoder devices using a
659 	 * direct assignment of connector->encoder = encoder. This connection
660 	 * is a logical one and the responsibility of the core, so drivers are
661 	 * expected not to mess with this.
662 	 *
663 	 * Note that the error return should've been enough here, but a large
664 	 * majority of drivers ignores the return value, so add in a big WARN
665 	 * to get people's attention.
666 	 */
667 	if (WARN_ON(connector->encoder))
668 		return -EINVAL;
669 
670 	connector->possible_encoders |= drm_encoder_mask(encoder);
671 
672 	return 0;
673 }
674 EXPORT_SYMBOL(drm_connector_attach_encoder);
675 
676 /**
677  * drm_connector_has_possible_encoder - check if the connector and encoder are
678  * associated with each other
679  * @connector: the connector
680  * @encoder: the encoder
681  *
682  * Returns:
683  * True if @encoder is one of the possible encoders for @connector.
684  */
685 bool drm_connector_has_possible_encoder(struct drm_connector *connector,
686 					struct drm_encoder *encoder)
687 {
688 	return connector->possible_encoders & drm_encoder_mask(encoder);
689 }
690 EXPORT_SYMBOL(drm_connector_has_possible_encoder);
691 
692 static void drm_mode_remove(struct drm_connector *connector,
693 			    struct drm_display_mode *mode)
694 {
695 	list_del(&mode->head);
696 	drm_mode_destroy(connector->dev, mode);
697 }
698 
699 /**
700  * drm_connector_cleanup - cleans up an initialised connector
701  * @connector: connector to cleanup
702  *
703  * Cleans up the connector but doesn't free the object.
704  */
705 void drm_connector_cleanup(struct drm_connector *connector)
706 {
707 	struct drm_device *dev = connector->dev;
708 	struct drm_display_mode *mode, *t;
709 
710 	/* The connector should have been removed from userspace long before
711 	 * it is finally destroyed.
712 	 */
713 	if (WARN_ON(connector->registration_state ==
714 		    DRM_CONNECTOR_REGISTERED))
715 		drm_connector_unregister(connector);
716 
717 	if (connector->privacy_screen) {
718 		drm_privacy_screen_put(connector->privacy_screen);
719 		connector->privacy_screen = NULL;
720 	}
721 
722 	if (connector->tile_group) {
723 		drm_mode_put_tile_group(dev, connector->tile_group);
724 		connector->tile_group = NULL;
725 	}
726 
727 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
728 		drm_mode_remove(connector, mode);
729 
730 	list_for_each_entry_safe(mode, t, &connector->modes, head)
731 		drm_mode_remove(connector, mode);
732 
733 	ida_free(&drm_connector_enum_list[connector->connector_type].ida,
734 			  connector->connector_type_id);
735 
736 	ida_free(&dev->mode_config.connector_ida, connector->index);
737 
738 	kfree(connector->display_info.bus_formats);
739 	kfree(connector->display_info.vics);
740 	drm_mode_object_unregister(dev, &connector->base);
741 	kfree(connector->name);
742 	connector->name = NULL;
743 	fwnode_handle_put(connector->fwnode);
744 	connector->fwnode = NULL;
745 
746 	drm_connector_remove(connector);
747 
748 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
749 	if (connector->state && connector->funcs->atomic_destroy_state)
750 		connector->funcs->atomic_destroy_state(connector,
751 						       connector->state);
752 
753 	mutex_destroy(&connector->hdmi.infoframes.lock);
754 	mutex_destroy(&connector->mutex);
755 
756 	memset(connector, 0, sizeof(*connector));
757 
758 	if (dev->registered)
759 		drm_sysfs_hotplug_event(dev);
760 }
761 EXPORT_SYMBOL(drm_connector_cleanup);
762 
763 /**
764  * drm_connector_register - register a connector
765  * @connector: the connector to register
766  *
767  * Register userspace interfaces for a connector. Drivers shouldn't call this
768  * function. Static connectors will be registered automatically by DRM core
769  * from drm_dev_register(), dynamic connectors (MST) should be registered by
770  * drivers calling drm_connector_dynamic_register().
771  *
772  * When the connector is no longer available, callers must call
773  * drm_connector_unregister().
774  *
775  * Note: Existing uses of this function in drivers should be a nop already and
776  * are scheduled to be removed.
777  *
778  * Returns:
779  * Zero on success, error code on failure.
780  */
781 int drm_connector_register(struct drm_connector *connector)
782 {
783 	int ret = 0;
784 
785 	if (!connector->dev->registered)
786 		return 0;
787 
788 	mutex_lock(&connector->mutex);
789 	if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
790 		goto unlock;
791 
792 	ret = drm_sysfs_connector_add(connector);
793 	if (ret)
794 		goto unlock;
795 
796 	drm_debugfs_connector_add(connector);
797 
798 	if (connector->funcs->late_register) {
799 		ret = connector->funcs->late_register(connector);
800 		if (ret)
801 			goto err_debugfs;
802 	}
803 
804 	ret = drm_sysfs_connector_add_late(connector);
805 	if (ret)
806 		goto err_late_register;
807 
808 	drm_mode_object_register(connector->dev, &connector->base);
809 
810 	connector->registration_state = DRM_CONNECTOR_REGISTERED;
811 
812 	/* Let userspace know we have a new connector */
813 	drm_sysfs_connector_hotplug_event(connector);
814 
815 	if (connector->privacy_screen)
816 		drm_privacy_screen_register_notifier(connector->privacy_screen,
817 					   &connector->privacy_screen_notifier);
818 
819 	mutex_lock(&connector_list_lock);
820 	list_add_tail(&connector->global_connector_list_entry, &connector_list);
821 	mutex_unlock(&connector_list_lock);
822 	goto unlock;
823 
824 err_late_register:
825 	if (connector->funcs->early_unregister)
826 		connector->funcs->early_unregister(connector);
827 err_debugfs:
828 	drm_debugfs_connector_remove(connector);
829 	drm_sysfs_connector_remove(connector);
830 unlock:
831 	mutex_unlock(&connector->mutex);
832 	return ret;
833 }
834 EXPORT_SYMBOL(drm_connector_register);
835 
836 /**
837  * drm_connector_dynamic_register - register a dynamic connector
838  * @connector: the connector to register
839  *
840  * Register userspace interfaces for a connector. Only call this for connectors
841  * initialized by calling drm_connector_dynamic_init(). All other connectors
842  * will be registered automatically when calling drm_dev_register().
843  *
844  * When the connector is no longer available the driver must call
845  * drm_connector_unregister().
846  *
847  * Returns:
848  * Zero on success, error code on failure.
849  */
850 int drm_connector_dynamic_register(struct drm_connector *connector)
851 {
852 	/* Was the connector inited already? */
853 	if (WARN_ON(!(connector->funcs && connector->funcs->destroy)))
854 		return -EINVAL;
855 
856 	drm_connector_add(connector);
857 
858 	return drm_connector_register(connector);
859 }
860 EXPORT_SYMBOL(drm_connector_dynamic_register);
861 
862 /**
863  * drm_connector_unregister - unregister a connector
864  * @connector: the connector to unregister
865  *
866  * Unregister userspace interfaces for a connector. Drivers should call this
867  * for dynamic connectors (MST) only, which were registered explicitly by
868  * calling drm_connector_dynamic_register(). All other - static - connectors
869  * will be unregistered automatically by DRM core and drivers shouldn't call
870  * this function for those.
871  *
872  * Note: Existing uses of this function in drivers for static connectors
873  * should be a nop already and are scheduled to be removed.
874  */
875 void drm_connector_unregister(struct drm_connector *connector)
876 {
877 	mutex_lock(&connector->mutex);
878 	if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
879 		mutex_unlock(&connector->mutex);
880 		return;
881 	}
882 
883 	mutex_lock(&connector_list_lock);
884 	list_del_init(&connector->global_connector_list_entry);
885 	mutex_unlock(&connector_list_lock);
886 
887 	if (connector->privacy_screen)
888 		drm_privacy_screen_unregister_notifier(
889 					connector->privacy_screen,
890 					&connector->privacy_screen_notifier);
891 
892 	drm_sysfs_connector_remove_early(connector);
893 
894 	if (connector->funcs->early_unregister)
895 		connector->funcs->early_unregister(connector);
896 
897 	drm_debugfs_connector_remove(connector);
898 	drm_sysfs_connector_remove(connector);
899 
900 	connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
901 	mutex_unlock(&connector->mutex);
902 }
903 EXPORT_SYMBOL(drm_connector_unregister);
904 
905 void drm_connector_unregister_all(struct drm_device *dev)
906 {
907 	struct drm_connector *connector;
908 	struct drm_connector_list_iter conn_iter;
909 
910 	drm_connector_list_iter_begin(dev, &conn_iter);
911 	drm_for_each_connector_iter(connector, &conn_iter)
912 		drm_connector_unregister(connector);
913 	drm_connector_list_iter_end(&conn_iter);
914 }
915 
916 int drm_connector_register_all(struct drm_device *dev)
917 {
918 	struct drm_connector *connector;
919 	struct drm_connector_list_iter conn_iter;
920 	int ret = 0;
921 
922 	drm_connector_list_iter_begin(dev, &conn_iter);
923 	drm_for_each_connector_iter(connector, &conn_iter) {
924 		ret = drm_connector_register(connector);
925 		if (ret)
926 			break;
927 	}
928 	drm_connector_list_iter_end(&conn_iter);
929 
930 	if (ret)
931 		drm_connector_unregister_all(dev);
932 	return ret;
933 }
934 
935 /**
936  * drm_get_connector_status_name - return a string for connector status
937  * @status: connector status to compute name of
938  *
939  * In contrast to the other drm_get_*_name functions this one here returns a
940  * const pointer and hence is threadsafe.
941  *
942  * Returns: connector status string
943  */
944 const char *drm_get_connector_status_name(enum drm_connector_status status)
945 {
946 	if (status == connector_status_connected)
947 		return "connected";
948 	else if (status == connector_status_disconnected)
949 		return "disconnected";
950 	else
951 		return "unknown";
952 }
953 EXPORT_SYMBOL(drm_get_connector_status_name);
954 
955 /**
956  * drm_get_connector_force_name - return a string for connector force
957  * @force: connector force to get name of
958  *
959  * Returns: const pointer to name.
960  */
961 const char *drm_get_connector_force_name(enum drm_connector_force force)
962 {
963 	switch (force) {
964 	case DRM_FORCE_UNSPECIFIED:
965 		return "unspecified";
966 	case DRM_FORCE_OFF:
967 		return "off";
968 	case DRM_FORCE_ON:
969 		return "on";
970 	case DRM_FORCE_ON_DIGITAL:
971 		return "digital";
972 	default:
973 		return "unknown";
974 	}
975 }
976 
977 #ifdef CONFIG_LOCKDEP
978 static struct lockdep_map connector_list_iter_dep_map = {
979 	.name = "drm_connector_list_iter"
980 };
981 #endif
982 
983 /**
984  * drm_connector_list_iter_begin - initialize a connector_list iterator
985  * @dev: DRM device
986  * @iter: connector_list iterator
987  *
988  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
989  * must always be cleaned up again by calling drm_connector_list_iter_end().
990  * Iteration itself happens using drm_connector_list_iter_next() or
991  * drm_for_each_connector_iter().
992  */
993 void drm_connector_list_iter_begin(struct drm_device *dev,
994 				   struct drm_connector_list_iter *iter)
995 {
996 	iter->dev = dev;
997 	iter->conn = NULL;
998 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
999 }
1000 EXPORT_SYMBOL(drm_connector_list_iter_begin);
1001 
1002 /*
1003  * Extra-safe connector put function that works in any context. Should only be
1004  * used from the connector_iter functions, where we never really expect to
1005  * actually release the connector when dropping our final reference.
1006  */
1007 static void
1008 __drm_connector_put_safe(struct drm_connector *conn)
1009 {
1010 	struct drm_mode_config *config = &conn->dev->mode_config;
1011 
1012 	lockdep_assert_held(&config->connector_list_lock);
1013 
1014 	if (!refcount_dec_and_test(&conn->base.refcount.refcount))
1015 		return;
1016 
1017 	llist_add(&conn->free_node, &config->connector_free_list);
1018 	schedule_work(&config->connector_free_work);
1019 }
1020 
1021 /**
1022  * drm_connector_list_iter_next - return next connector
1023  * @iter: connector_list iterator
1024  *
1025  * Returns: the next connector for @iter, or NULL when the list walk has
1026  * completed.
1027  */
1028 struct drm_connector *
1029 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
1030 {
1031 	struct drm_connector *old_conn = iter->conn;
1032 	struct drm_mode_config *config = &iter->dev->mode_config;
1033 	struct list_head *lhead;
1034 	unsigned long flags;
1035 
1036 	spin_lock_irqsave(&config->connector_list_lock, flags);
1037 	lhead = old_conn ? &old_conn->head : &config->connector_list;
1038 
1039 	do {
1040 		if (lhead->next == &config->connector_list) {
1041 			iter->conn = NULL;
1042 			break;
1043 		}
1044 
1045 		lhead = lhead->next;
1046 		iter->conn = list_entry(lhead, struct drm_connector, head);
1047 
1048 		/* loop until it's not a zombie connector */
1049 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
1050 
1051 	if (old_conn)
1052 		__drm_connector_put_safe(old_conn);
1053 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
1054 
1055 	return iter->conn;
1056 }
1057 EXPORT_SYMBOL(drm_connector_list_iter_next);
1058 
1059 /**
1060  * drm_connector_list_iter_end - tear down a connector_list iterator
1061  * @iter: connector_list iterator
1062  *
1063  * Tears down @iter and releases any resources (like &drm_connector references)
1064  * acquired while walking the list. This must always be called, both when the
1065  * iteration completes fully or when it was aborted without walking the entire
1066  * list.
1067  */
1068 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
1069 {
1070 	struct drm_mode_config *config = &iter->dev->mode_config;
1071 	unsigned long flags;
1072 
1073 	iter->dev = NULL;
1074 	if (iter->conn) {
1075 		spin_lock_irqsave(&config->connector_list_lock, flags);
1076 		__drm_connector_put_safe(iter->conn);
1077 		spin_unlock_irqrestore(&config->connector_list_lock, flags);
1078 	}
1079 	lock_release(&connector_list_iter_dep_map, _RET_IP_);
1080 }
1081 EXPORT_SYMBOL(drm_connector_list_iter_end);
1082 
1083 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
1084 	{ SubPixelUnknown, "Unknown" },
1085 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
1086 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
1087 	{ SubPixelVerticalRGB, "Vertical RGB" },
1088 	{ SubPixelVerticalBGR, "Vertical BGR" },
1089 	{ SubPixelNone, "None" },
1090 };
1091 
1092 /**
1093  * drm_get_subpixel_order_name - return a string for a given subpixel enum
1094  * @order: enum of subpixel_order
1095  *
1096  * Note you could abuse this and return something out of bounds, but that
1097  * would be a caller error.  No unscrubbed user data should make it here.
1098  *
1099  * Returns: string describing an enumerated subpixel property
1100  */
1101 const char *drm_get_subpixel_order_name(enum subpixel_order order)
1102 {
1103 	return drm_subpixel_enum_list[order].name;
1104 }
1105 EXPORT_SYMBOL(drm_get_subpixel_order_name);
1106 
1107 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
1108 	{ DRM_MODE_DPMS_ON, "On" },
1109 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
1110 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
1111 	{ DRM_MODE_DPMS_OFF, "Off" }
1112 };
1113 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
1114 
1115 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
1116 	{ DRM_MODE_LINK_STATUS_GOOD, "Good" },
1117 	{ DRM_MODE_LINK_STATUS_BAD, "Bad" },
1118 };
1119 
1120 /**
1121  * drm_display_info_set_bus_formats - set the supported bus formats
1122  * @info: display info to store bus formats in
1123  * @formats: array containing the supported bus formats
1124  * @num_formats: the number of entries in the fmts array
1125  *
1126  * Store the supported bus formats in display info structure.
1127  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
1128  * a full list of available formats.
1129  *
1130  * Returns:
1131  * 0 on success or a negative error code on failure.
1132  */
1133 int drm_display_info_set_bus_formats(struct drm_display_info *info,
1134 				     const u32 *formats,
1135 				     unsigned int num_formats)
1136 {
1137 	u32 *fmts = NULL;
1138 
1139 	if (!formats && num_formats)
1140 		return -EINVAL;
1141 
1142 	if (formats && num_formats) {
1143 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
1144 			       GFP_KERNEL);
1145 		if (!fmts)
1146 			return -ENOMEM;
1147 	}
1148 
1149 	kfree(info->bus_formats);
1150 	info->bus_formats = fmts;
1151 	info->num_bus_formats = num_formats;
1152 
1153 	return 0;
1154 }
1155 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
1156 
1157 /* Optional connector properties. */
1158 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
1159 	{ DRM_MODE_SCALE_NONE, "None" },
1160 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
1161 	{ DRM_MODE_SCALE_CENTER, "Center" },
1162 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
1163 };
1164 
1165 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
1166 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
1167 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
1168 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
1169 };
1170 
1171 static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
1172 	{ DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
1173 	{ DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
1174 	{ DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
1175 	{ DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
1176 	{ DRM_MODE_CONTENT_TYPE_GAME, "Game" },
1177 };
1178 
1179 static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
1180 	{ DRM_MODE_PANEL_ORIENTATION_NORMAL,	"Normal"	},
1181 	{ DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP,	"Upside Down"	},
1182 	{ DRM_MODE_PANEL_ORIENTATION_LEFT_UP,	"Left Side Up"	},
1183 	{ DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,	"Right Side Up"	},
1184 };
1185 
1186 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
1187 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1188 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
1189 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
1190 };
1191 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
1192 
1193 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
1194 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
1195 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
1196 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
1197 };
1198 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
1199 		 drm_dvi_i_subconnector_enum_list)
1200 
1201 static const struct drm_prop_enum_list drm_tv_mode_enum_list[] = {
1202 	{ DRM_MODE_TV_MODE_NTSC, "NTSC" },
1203 	{ DRM_MODE_TV_MODE_NTSC_443, "NTSC-443" },
1204 	{ DRM_MODE_TV_MODE_NTSC_J, "NTSC-J" },
1205 	{ DRM_MODE_TV_MODE_PAL, "PAL" },
1206 	{ DRM_MODE_TV_MODE_PAL_M, "PAL-M" },
1207 	{ DRM_MODE_TV_MODE_PAL_N, "PAL-N" },
1208 	{ DRM_MODE_TV_MODE_SECAM, "SECAM" },
1209 	{ DRM_MODE_TV_MODE_MONOCHROME, "Mono" },
1210 };
1211 DRM_ENUM_NAME_FN(drm_get_tv_mode_name, drm_tv_mode_enum_list)
1212 
1213 /**
1214  * drm_get_tv_mode_from_name - Translates a TV mode name into its enum value
1215  * @name: TV Mode name we want to convert
1216  * @len: Length of @name
1217  *
1218  * Translates @name into an enum drm_connector_tv_mode.
1219  *
1220  * Returns: the enum value on success, a negative errno otherwise.
1221  */
1222 int drm_get_tv_mode_from_name(const char *name, size_t len)
1223 {
1224 	unsigned int i;
1225 
1226 	for (i = 0; i < ARRAY_SIZE(drm_tv_mode_enum_list); i++) {
1227 		const struct drm_prop_enum_list *item = &drm_tv_mode_enum_list[i];
1228 
1229 		if (strlen(item->name) == len && !strncmp(item->name, name, len))
1230 			return item->type;
1231 	}
1232 
1233 	return -EINVAL;
1234 }
1235 EXPORT_SYMBOL(drm_get_tv_mode_from_name);
1236 
1237 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
1238 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1239 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1240 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1241 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1242 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1243 };
1244 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
1245 
1246 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
1247 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
1248 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1249 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1250 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1251 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1252 };
1253 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
1254 		 drm_tv_subconnector_enum_list)
1255 
1256 static const struct drm_prop_enum_list drm_dp_subconnector_enum_list[] = {
1257 	{ DRM_MODE_SUBCONNECTOR_Unknown,     "Unknown"   }, /* DVI-I, TV-out and DP */
1258 	{ DRM_MODE_SUBCONNECTOR_VGA,	     "VGA"       }, /* DP */
1259 	{ DRM_MODE_SUBCONNECTOR_DVID,	     "DVI-D"     }, /* DP */
1260 	{ DRM_MODE_SUBCONNECTOR_HDMIA,	     "HDMI"      }, /* DP */
1261 	{ DRM_MODE_SUBCONNECTOR_DisplayPort, "DP"        }, /* DP */
1262 	{ DRM_MODE_SUBCONNECTOR_Wireless,    "Wireless"  }, /* DP */
1263 	{ DRM_MODE_SUBCONNECTOR_Native,	     "Native"    }, /* DP */
1264 };
1265 
1266 DRM_ENUM_NAME_FN(drm_get_dp_subconnector_name,
1267 		 drm_dp_subconnector_enum_list)
1268 
1269 
1270 static const char * const colorspace_names[] = {
1271 	/* For Default case, driver will set the colorspace */
1272 	[DRM_MODE_COLORIMETRY_DEFAULT] = "Default",
1273 	/* Standard Definition Colorimetry based on CEA 861 */
1274 	[DRM_MODE_COLORIMETRY_SMPTE_170M_YCC] = "SMPTE_170M_YCC",
1275 	[DRM_MODE_COLORIMETRY_BT709_YCC] = "BT709_YCC",
1276 	/* Standard Definition Colorimetry based on IEC 61966-2-4 */
1277 	[DRM_MODE_COLORIMETRY_XVYCC_601] = "XVYCC_601",
1278 	/* High Definition Colorimetry based on IEC 61966-2-4 */
1279 	[DRM_MODE_COLORIMETRY_XVYCC_709] = "XVYCC_709",
1280 	/* Colorimetry based on IEC 61966-2-1/Amendment 1 */
1281 	[DRM_MODE_COLORIMETRY_SYCC_601] = "SYCC_601",
1282 	/* Colorimetry based on IEC 61966-2-5 [33] */
1283 	[DRM_MODE_COLORIMETRY_OPYCC_601] = "opYCC_601",
1284 	/* Colorimetry based on IEC 61966-2-5 */
1285 	[DRM_MODE_COLORIMETRY_OPRGB] = "opRGB",
1286 	/* Colorimetry based on ITU-R BT.2020 */
1287 	[DRM_MODE_COLORIMETRY_BT2020_CYCC] = "BT2020_CYCC",
1288 	/* Colorimetry based on ITU-R BT.2020 */
1289 	[DRM_MODE_COLORIMETRY_BT2020_RGB] = "BT2020_RGB",
1290 	/* Colorimetry based on ITU-R BT.2020 */
1291 	[DRM_MODE_COLORIMETRY_BT2020_YCC] = "BT2020_YCC",
1292 	/* Added as part of Additional Colorimetry Extension in 861.G */
1293 	[DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65] = "DCI-P3_RGB_D65",
1294 	[DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER] = "DCI-P3_RGB_Theater",
1295 	[DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED] = "RGB_WIDE_FIXED",
1296 	/* Colorimetry based on scRGB (IEC 61966-2-2) */
1297 	[DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT] = "RGB_WIDE_FLOAT",
1298 	[DRM_MODE_COLORIMETRY_BT601_YCC] = "BT601_YCC",
1299 };
1300 
1301 /**
1302  * drm_get_colorspace_name - return a string for color encoding
1303  * @colorspace: color space to compute name of
1304  *
1305  * In contrast to the other drm_get_*_name functions this one here returns a
1306  * const pointer and hence is threadsafe.
1307  */
1308 const char *drm_get_colorspace_name(enum drm_colorspace colorspace)
1309 {
1310 	if (colorspace < ARRAY_SIZE(colorspace_names) && colorspace_names[colorspace])
1311 		return colorspace_names[colorspace];
1312 	else
1313 		return "(null)";
1314 }
1315 
1316 static const u32 hdmi_colorspaces =
1317 	BIT(DRM_MODE_COLORIMETRY_SMPTE_170M_YCC) |
1318 	BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
1319 	BIT(DRM_MODE_COLORIMETRY_XVYCC_601) |
1320 	BIT(DRM_MODE_COLORIMETRY_XVYCC_709) |
1321 	BIT(DRM_MODE_COLORIMETRY_SYCC_601) |
1322 	BIT(DRM_MODE_COLORIMETRY_OPYCC_601) |
1323 	BIT(DRM_MODE_COLORIMETRY_OPRGB) |
1324 	BIT(DRM_MODE_COLORIMETRY_BT2020_CYCC) |
1325 	BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
1326 	BIT(DRM_MODE_COLORIMETRY_BT2020_YCC) |
1327 	BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65) |
1328 	BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER);
1329 
1330 /*
1331  * As per DP 1.4a spec, 2.2.5.7.5 VSC SDP Payload for Pixel Encoding/Colorimetry
1332  * Format Table 2-120
1333  */
1334 static const u32 dp_colorspaces =
1335 	BIT(DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED) |
1336 	BIT(DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT) |
1337 	BIT(DRM_MODE_COLORIMETRY_OPRGB) |
1338 	BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65) |
1339 	BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
1340 	BIT(DRM_MODE_COLORIMETRY_BT601_YCC) |
1341 	BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
1342 	BIT(DRM_MODE_COLORIMETRY_XVYCC_601) |
1343 	BIT(DRM_MODE_COLORIMETRY_XVYCC_709) |
1344 	BIT(DRM_MODE_COLORIMETRY_SYCC_601) |
1345 	BIT(DRM_MODE_COLORIMETRY_OPYCC_601) |
1346 	BIT(DRM_MODE_COLORIMETRY_BT2020_CYCC) |
1347 	BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
1348 
1349 static const struct drm_prop_enum_list broadcast_rgb_names[] = {
1350 	{ DRM_HDMI_BROADCAST_RGB_AUTO, "Automatic" },
1351 	{ DRM_HDMI_BROADCAST_RGB_FULL, "Full" },
1352 	{ DRM_HDMI_BROADCAST_RGB_LIMITED, "Limited 16:235" },
1353 };
1354 
1355 /*
1356  * drm_hdmi_connector_get_broadcast_rgb_name - Return a string for HDMI connector RGB broadcast selection
1357  * @broadcast_rgb: Broadcast RGB selection to compute name of
1358  *
1359  * Returns: the name of the Broadcast RGB selection, or NULL if the type
1360  * is not valid.
1361  */
1362 const char *
1363 drm_hdmi_connector_get_broadcast_rgb_name(enum drm_hdmi_broadcast_rgb broadcast_rgb)
1364 {
1365 	if (broadcast_rgb >= ARRAY_SIZE(broadcast_rgb_names))
1366 		return NULL;
1367 
1368 	return broadcast_rgb_names[broadcast_rgb].name;
1369 }
1370 EXPORT_SYMBOL(drm_hdmi_connector_get_broadcast_rgb_name);
1371 
1372 static const char * const output_format_str[] = {
1373 	[HDMI_COLORSPACE_RGB]		= "RGB",
1374 	[HDMI_COLORSPACE_YUV420]	= "YUV 4:2:0",
1375 	[HDMI_COLORSPACE_YUV422]	= "YUV 4:2:2",
1376 	[HDMI_COLORSPACE_YUV444]	= "YUV 4:4:4",
1377 };
1378 
1379 /*
1380  * drm_hdmi_connector_get_output_format_name() - Return a string for HDMI connector output format
1381  * @fmt: Output format to compute name of
1382  *
1383  * Returns: the name of the output format, or NULL if the type is not
1384  * valid.
1385  */
1386 const char *
1387 drm_hdmi_connector_get_output_format_name(enum hdmi_colorspace fmt)
1388 {
1389 	if (fmt >= ARRAY_SIZE(output_format_str))
1390 		return NULL;
1391 
1392 	return output_format_str[fmt];
1393 }
1394 EXPORT_SYMBOL(drm_hdmi_connector_get_output_format_name);
1395 
1396 /**
1397  * DOC: standard connector properties
1398  *
1399  * DRM connectors have a few standardized properties:
1400  *
1401  * EDID:
1402  * 	Blob property which contains the current EDID read from the sink. This
1403  * 	is useful to parse sink identification information like vendor, model
1404  * 	and serial. Drivers should update this property by calling
1405  * 	drm_connector_update_edid_property(), usually after having parsed
1406  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
1407  * 	property.
1408  *
1409  * 	User-space should not parse the EDID to obtain information exposed via
1410  * 	other KMS properties (because the kernel might apply limits, quirks or
1411  * 	fixups to the EDID). For instance, user-space should not try to parse
1412  * 	mode lists from the EDID.
1413  * DPMS:
1414  * 	Legacy property for setting the power state of the connector. For atomic
1415  * 	drivers this is only provided for backwards compatibility with existing
1416  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
1417  * 	connector is linked to. Drivers should never set this property directly,
1418  * 	it is handled by the DRM core by calling the &drm_connector_funcs.dpms
1419  * 	callback. For atomic drivers the remapping to the "ACTIVE" property is
1420  * 	implemented in the DRM core.
1421  *
1422  * 	Note that this property cannot be set through the MODE_ATOMIC ioctl,
1423  * 	userspace must use "ACTIVE" on the CRTC instead.
1424  *
1425  * 	WARNING:
1426  *
1427  * 	For userspace also running on legacy drivers the "DPMS" semantics are a
1428  * 	lot more complicated. First, userspace cannot rely on the "DPMS" value
1429  * 	returned by the GETCONNECTOR actually reflecting reality, because many
1430  * 	drivers fail to update it. For atomic drivers this is taken care of in
1431  * 	drm_atomic_helper_update_legacy_modeset_state().
1432  *
1433  * 	The second issue is that the DPMS state is only well-defined when the
1434  * 	connector is connected to a CRTC. In atomic the DRM core enforces that
1435  * 	"ACTIVE" is off in such a case, no such checks exists for "DPMS".
1436  *
1437  * 	Finally, when enabling an output using the legacy SETCONFIG ioctl then
1438  * 	"DPMS" is forced to ON. But see above, that might not be reflected in
1439  * 	the software value on legacy drivers.
1440  *
1441  * 	Summarizing: Only set "DPMS" when the connector is known to be enabled,
1442  * 	assume that a successful SETCONFIG call also sets "DPMS" to on, and
1443  * 	never read back the value of "DPMS" because it can be incorrect.
1444  * PATH:
1445  * 	Connector path property to identify how this sink is physically
1446  * 	connected. Used by DP MST. This should be set by calling
1447  * 	drm_connector_set_path_property(), in the case of DP MST with the
1448  * 	path property the MST manager created. Userspace cannot change this
1449  * 	property.
1450  *
1451  * 	In the case of DP MST, the property has the format
1452  * 	``mst:<parent>-<ports>`` where ``<parent>`` is the KMS object ID of the
1453  * 	parent connector and ``<ports>`` is a hyphen-separated list of DP MST
1454  * 	port numbers. Note, KMS object IDs are not guaranteed to be stable
1455  * 	across reboots.
1456  * TILE:
1457  * 	Connector tile group property to indicate how a set of DRM connector
1458  * 	compose together into one logical screen. This is used by both high-res
1459  * 	external screens (often only using a single cable, but exposing multiple
1460  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
1461  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
1462  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
1463  * 	tiling and virtualise both &drm_crtc and &drm_plane if needed. Drivers
1464  * 	should update this value using drm_connector_set_tile_property().
1465  * 	Userspace cannot change this property.
1466  * link-status:
1467  *      Connector link-status property to indicate the status of link. The
1468  *      default value of link-status is "GOOD". If something fails during or
1469  *      after modeset, the kernel driver may set this to "BAD" and issue a
1470  *      hotplug uevent. Drivers should update this value using
1471  *      drm_connector_set_link_status_property().
1472  *
1473  *      When user-space receives the hotplug uevent and detects a "BAD"
1474  *      link-status, the sink doesn't receive pixels anymore (e.g. the screen
1475  *      becomes completely black). The list of available modes may have
1476  *      changed. User-space is expected to pick a new mode if the current one
1477  *      has disappeared and perform a new modeset with link-status set to
1478  *      "GOOD" to re-enable the connector.
1479  *
1480  *      If multiple connectors share the same CRTC and one of them gets a "BAD"
1481  *      link-status, the other are unaffected (ie. the sinks still continue to
1482  *      receive pixels).
1483  *
1484  *      When user-space performs an atomic commit on a connector with a "BAD"
1485  *      link-status without resetting the property to "GOOD", the sink may
1486  *      still not receive pixels. When user-space performs an atomic commit
1487  *      which resets the link-status property to "GOOD" without the
1488  *      ALLOW_MODESET flag set, it might fail because a modeset is required.
1489  *
1490  *      User-space can only change link-status to "GOOD", changing it to "BAD"
1491  *      is a no-op.
1492  *
1493  *      For backwards compatibility with non-atomic userspace the kernel
1494  *      tries to automatically set the link-status back to "GOOD" in the
1495  *      SETCRTC IOCTL. This might fail if the mode is no longer valid, similar
1496  *      to how it might fail if a different screen has been connected in the
1497  *      interim.
1498  * non_desktop:
1499  * 	Indicates the output should be ignored for purposes of displaying a
1500  * 	standard desktop environment or console. This is most likely because
1501  * 	the output device is not rectilinear.
1502  * Content Protection:
1503  *	This property is used by userspace to request the kernel protect future
1504  *	content communicated over the link. When requested, kernel will apply
1505  *	the appropriate means of protection (most often HDCP), and use the
1506  *	property to tell userspace the protection is active.
1507  *
1508  *	Drivers can set this up by calling
1509  *	drm_connector_attach_content_protection_property() on initialization.
1510  *
1511  *	The value of this property can be one of the following:
1512  *
1513  *	DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
1514  *		The link is not protected, content is transmitted in the clear.
1515  *	DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
1516  *		Userspace has requested content protection, but the link is not
1517  *		currently protected. When in this state, kernel should enable
1518  *		Content Protection as soon as possible.
1519  *	DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
1520  *		Userspace has requested content protection, and the link is
1521  *		protected. Only the driver can set the property to this value.
1522  *		If userspace attempts to set to ENABLED, kernel will return
1523  *		-EINVAL.
1524  *
1525  *	A few guidelines:
1526  *
1527  *	- DESIRED state should be preserved until userspace de-asserts it by
1528  *	  setting the property to UNDESIRED. This means ENABLED should only
1529  *	  transition to UNDESIRED when the user explicitly requests it.
1530  *	- If the state is DESIRED, kernel should attempt to re-authenticate the
1531  *	  link whenever possible. This includes across disable/enable, dpms,
1532  *	  hotplug, downstream device changes, link status failures, etc..
1533  *	- Kernel sends uevent with the connector id and property id through
1534  *	  @drm_hdcp_update_content_protection, upon below kernel triggered
1535  *	  scenarios:
1536  *
1537  *		- DESIRED -> ENABLED (authentication success)
1538  *		- ENABLED -> DESIRED (termination of authentication)
1539  *	- Please note no uevents for userspace triggered property state changes,
1540  *	  which can't fail such as
1541  *
1542  *		- DESIRED/ENABLED -> UNDESIRED
1543  *		- UNDESIRED -> DESIRED
1544  *	- Userspace is responsible for polling the property or listen to uevents
1545  *	  to determine when the value transitions from ENABLED to DESIRED.
1546  *	  This signifies the link is no longer protected and userspace should
1547  *	  take appropriate action (whatever that might be).
1548  *
1549  * HDCP Content Type:
1550  *	This Enum property is used by the userspace to declare the content type
1551  *	of the display stream, to kernel. Here display stream stands for any
1552  *	display content that userspace intended to display through HDCP
1553  *	encryption.
1554  *
1555  *	Content Type of a stream is decided by the owner of the stream, as
1556  *	"HDCP Type0" or "HDCP Type1".
1557  *
1558  *	The value of the property can be one of the below:
1559  *	  - "HDCP Type0": DRM_MODE_HDCP_CONTENT_TYPE0 = 0
1560  *	  - "HDCP Type1": DRM_MODE_HDCP_CONTENT_TYPE1 = 1
1561  *
1562  *	When kernel starts the HDCP authentication (see "Content Protection"
1563  *	for details), it uses the content type in "HDCP Content Type"
1564  *	for performing the HDCP authentication with the display sink.
1565  *
1566  *	Please note in HDCP spec versions, a link can be authenticated with
1567  *	HDCP 2.2 for Content Type 0/Content Type 1. Where as a link can be
1568  *	authenticated with HDCP1.4 only for Content Type 0(though it is implicit
1569  *	in nature. As there is no reference for Content Type in HDCP1.4).
1570  *
1571  *	HDCP2.2 authentication protocol itself takes the "Content Type" as a
1572  *	parameter, which is a input for the DP HDCP2.2 encryption algo.
1573  *
1574  *	In case of Type 0 content protection request, kernel driver can choose
1575  *	either of HDCP spec versions 1.4 and 2.2. When HDCP2.2 is used for
1576  *	"HDCP Type 0", a HDCP 2.2 capable repeater in the downstream can send
1577  *	that content to a HDCP 1.4 authenticated HDCP sink (Type0 link).
1578  *	But if the content is classified as "HDCP Type 1", above mentioned
1579  *	HDCP 2.2 repeater wont send the content to the HDCP sink as it can't
1580  *	authenticate the HDCP1.4 capable sink for "HDCP Type 1".
1581  *
1582  *	Please note userspace can be ignorant of the HDCP versions used by the
1583  *	kernel driver to achieve the "HDCP Content Type".
1584  *
1585  *	At current scenario, classifying a content as Type 1 ensures that the
1586  *	content will be displayed only through the HDCP2.2 encrypted link.
1587  *
1588  *	Note that the HDCP Content Type property is introduced at HDCP 2.2, and
1589  *	defaults to type 0. It is only exposed by drivers supporting HDCP 2.2
1590  *	(hence supporting Type 0 and Type 1). Based on how next versions of
1591  *	HDCP specs are defined content Type could be used for higher versions
1592  *	too.
1593  *
1594  *	If content type is changed when "Content Protection" is not UNDESIRED,
1595  *	then kernel will disable the HDCP and re-enable with new type in the
1596  *	same atomic commit. And when "Content Protection" is ENABLED, it means
1597  *	that link is HDCP authenticated and encrypted, for the transmission of
1598  *	the Type of stream mentioned at "HDCP Content Type".
1599  *
1600  * HDR_OUTPUT_METADATA:
1601  *	Connector property to enable userspace to send HDR Metadata to
1602  *	driver. This metadata is based on the composition and blending
1603  *	policies decided by user, taking into account the hardware and
1604  *	sink capabilities. The driver gets this metadata and creates a
1605  *	Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
1606  *	SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
1607  *	sent to sink. This notifies the sink of the upcoming frame's Color
1608  *	Encoding and Luminance parameters.
1609  *
1610  *	Userspace first need to detect the HDR capabilities of sink by
1611  *	reading and parsing the EDID. Details of HDR metadata for HDMI
1612  *	are added in CTA 861.G spec. For DP , its defined in VESA DP
1613  *	Standard v1.4. It needs to then get the metadata information
1614  *	of the video/game/app content which are encoded in HDR (basically
1615  *	using HDR transfer functions). With this information it needs to
1616  *	decide on a blending policy and compose the relevant
1617  *	layers/overlays into a common format. Once this blending is done,
1618  *	userspace will be aware of the metadata of the composed frame to
1619  *	be send to sink. It then uses this property to communicate this
1620  *	metadata to driver which then make a Infoframe packet and sends
1621  *	to sink based on the type of encoder connected.
1622  *
1623  *	Userspace will be responsible to do Tone mapping operation in case:
1624  *		- Some layers are HDR and others are SDR
1625  *		- HDR layers luminance is not same as sink
1626  *
1627  *	It will even need to do colorspace conversion and get all layers
1628  *	to one common colorspace for blending. It can use either GL, Media
1629  *	or display engine to get this done based on the capabilities of the
1630  *	associated hardware.
1631  *
1632  *	Driver expects metadata to be put in &struct hdr_output_metadata
1633  *	structure from userspace. This is received as blob and stored in
1634  *	&drm_connector_state.hdr_output_metadata. It parses EDID and saves the
1635  *	sink metadata in &struct hdr_sink_metadata, as
1636  *	&drm_connector.hdr_sink_metadata.  Driver uses
1637  *	drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
1638  *	hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
1639  *	HDMI encoder.
1640  *
1641  * max bpc:
1642  *	This range property is used by userspace to limit the bit depth. When
1643  *	used the driver would limit the bpc in accordance with the valid range
1644  *	supported by the hardware and sink. Drivers to use the function
1645  *	drm_connector_attach_max_bpc_property() to create and attach the
1646  *	property to the connector during initialization.
1647  *
1648  * Connectors also have one standardized atomic property:
1649  *
1650  * CRTC_ID:
1651  * 	Mode object ID of the &drm_crtc this connector should be connected to.
1652  *
1653  * Connectors for LCD panels may also have one standardized property:
1654  *
1655  * panel orientation:
1656  *	On some devices the LCD panel is mounted in the casing in such a way
1657  *	that the up/top side of the panel does not match with the top side of
1658  *	the device. Userspace can use this property to check for this.
1659  *	Note that input coordinates from touchscreens (input devices with
1660  *	INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
1661  *	coordinates, so if userspace rotates the picture to adjust for
1662  *	the orientation it must also apply the same transformation to the
1663  *	touchscreen input coordinates. This property is initialized by calling
1664  *	drm_connector_set_panel_orientation() or
1665  *	drm_connector_set_panel_orientation_with_quirk()
1666  *
1667  * scaling mode:
1668  *	This property defines how a non-native mode is upscaled to the native
1669  *	mode of an LCD panel:
1670  *
1671  *	None:
1672  *		No upscaling happens, scaling is left to the panel. Not all
1673  *		drivers expose this mode.
1674  *	Full:
1675  *		The output is upscaled to the full resolution of the panel,
1676  *		ignoring the aspect ratio.
1677  *	Center:
1678  *		No upscaling happens, the output is centered within the native
1679  *		resolution the panel.
1680  *	Full aspect:
1681  *		The output is upscaled to maximize either the width or height
1682  *		while retaining the aspect ratio.
1683  *
1684  *	This property should be set up by calling
1685  *	drm_connector_attach_scaling_mode_property(). Note that drivers
1686  *	can also expose this property to external outputs, in which case they
1687  *	must support "None", which should be the default (since external screens
1688  *	have a built-in scaler).
1689  *
1690  * subconnector:
1691  *	This property is used by DVI-I, TVout and DisplayPort to indicate different
1692  *	connector subtypes. Enum values more or less match with those from main
1693  *	connector types.
1694  *	For DVI-I and TVout there is also a matching property "select subconnector"
1695  *	allowing to switch between signal types.
1696  *	DP subconnector corresponds to a downstream port.
1697  *
1698  * privacy-screen sw-state, privacy-screen hw-state:
1699  *	These 2 optional properties can be used to query the state of the
1700  *	electronic privacy screen that is available on some displays; and in
1701  *	some cases also control the state. If a driver implements these
1702  *	properties then both properties must be present.
1703  *
1704  *	"privacy-screen hw-state" is read-only and reflects the actual state
1705  *	of the privacy-screen, possible values: "Enabled", "Disabled,
1706  *	"Enabled-locked", "Disabled-locked". The locked states indicate
1707  *	that the state cannot be changed through the DRM API. E.g. there
1708  *	might be devices where the firmware-setup options, or a hardware
1709  *	slider-switch, offer always on / off modes.
1710  *
1711  *	"privacy-screen sw-state" can be set to change the privacy-screen state
1712  *	when not locked. In this case the driver must update the hw-state
1713  *	property to reflect the new state on completion of the commit of the
1714  *	sw-state property. Setting the sw-state property when the hw-state is
1715  *	locked must be interpreted by the driver as a request to change the
1716  *	state to the set state when the hw-state becomes unlocked. E.g. if
1717  *	"privacy-screen hw-state" is "Enabled-locked" and the sw-state
1718  *	gets set to "Disabled" followed by the user unlocking the state by
1719  *	changing the slider-switch position, then the driver must set the
1720  *	state to "Disabled" upon receiving the unlock event.
1721  *
1722  *	In some cases the privacy-screen's actual state might change outside of
1723  *	control of the DRM code. E.g. there might be a firmware handled hotkey
1724  *	which toggles the actual state, or the actual state might be changed
1725  *	through another userspace API such as writing /proc/acpi/ibm/lcdshadow.
1726  *	In this case the driver must update both the hw-state and the sw-state
1727  *	to reflect the new value, overwriting any pending state requests in the
1728  *	sw-state. Any pending sw-state requests are thus discarded.
1729  *
1730  *	Note that the ability for the state to change outside of control of
1731  *	the DRM master process means that userspace must not cache the value
1732  *	of the sw-state. Caching the sw-state value and including it in later
1733  *	atomic commits may lead to overriding a state change done through e.g.
1734  *	a firmware handled hotkey. Therefor userspace must not include the
1735  *	privacy-screen sw-state in an atomic commit unless it wants to change
1736  *	its value.
1737  *
1738  * left margin, right margin, top margin, bottom margin:
1739  *	Add margins to the connector's viewport. This is typically used to
1740  *	mitigate overscan on TVs.
1741  *
1742  *	The value is the size in pixels of the black border which will be
1743  *	added. The attached CRTC's content will be scaled to fill the whole
1744  *	area inside the margin.
1745  *
1746  *	The margins configuration might be sent to the sink, e.g. via HDMI AVI
1747  *	InfoFrames.
1748  *
1749  *	Drivers can set up these properties by calling
1750  *	drm_mode_create_tv_margin_properties().
1751  */
1752 
1753 int drm_connector_create_standard_properties(struct drm_device *dev)
1754 {
1755 	struct drm_property *prop;
1756 
1757 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1758 				   DRM_MODE_PROP_IMMUTABLE,
1759 				   "EDID", 0);
1760 	if (!prop)
1761 		return -ENOMEM;
1762 	dev->mode_config.edid_property = prop;
1763 
1764 	prop = drm_property_create_enum(dev, 0,
1765 				   "DPMS", drm_dpms_enum_list,
1766 				   ARRAY_SIZE(drm_dpms_enum_list));
1767 	if (!prop)
1768 		return -ENOMEM;
1769 	dev->mode_config.dpms_property = prop;
1770 
1771 	prop = drm_property_create(dev,
1772 				   DRM_MODE_PROP_BLOB |
1773 				   DRM_MODE_PROP_IMMUTABLE,
1774 				   "PATH", 0);
1775 	if (!prop)
1776 		return -ENOMEM;
1777 	dev->mode_config.path_property = prop;
1778 
1779 	prop = drm_property_create(dev,
1780 				   DRM_MODE_PROP_BLOB |
1781 				   DRM_MODE_PROP_IMMUTABLE,
1782 				   "TILE", 0);
1783 	if (!prop)
1784 		return -ENOMEM;
1785 	dev->mode_config.tile_property = prop;
1786 
1787 	prop = drm_property_create_enum(dev, 0, "link-status",
1788 					drm_link_status_enum_list,
1789 					ARRAY_SIZE(drm_link_status_enum_list));
1790 	if (!prop)
1791 		return -ENOMEM;
1792 	dev->mode_config.link_status_property = prop;
1793 
1794 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
1795 	if (!prop)
1796 		return -ENOMEM;
1797 	dev->mode_config.non_desktop_property = prop;
1798 
1799 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1800 				   "HDR_OUTPUT_METADATA", 0);
1801 	if (!prop)
1802 		return -ENOMEM;
1803 	dev->mode_config.hdr_output_metadata_property = prop;
1804 
1805 	return 0;
1806 }
1807 
1808 /**
1809  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1810  * @dev: DRM device
1811  *
1812  * Called by a driver the first time a DVI-I connector is made.
1813  *
1814  * Returns: %0
1815  */
1816 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1817 {
1818 	struct drm_property *dvi_i_selector;
1819 	struct drm_property *dvi_i_subconnector;
1820 
1821 	if (dev->mode_config.dvi_i_select_subconnector_property)
1822 		return 0;
1823 
1824 	dvi_i_selector =
1825 		drm_property_create_enum(dev, 0,
1826 				    "select subconnector",
1827 				    drm_dvi_i_select_enum_list,
1828 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
1829 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1830 
1831 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1832 				    "subconnector",
1833 				    drm_dvi_i_subconnector_enum_list,
1834 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1835 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1836 
1837 	return 0;
1838 }
1839 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1840 
1841 /**
1842  * drm_connector_attach_dp_subconnector_property - create subconnector property for DP
1843  * @connector: drm_connector to attach property
1844  *
1845  * Called by a driver when DP connector is created.
1846  */
1847 void drm_connector_attach_dp_subconnector_property(struct drm_connector *connector)
1848 {
1849 	struct drm_mode_config *mode_config = &connector->dev->mode_config;
1850 
1851 	if (!mode_config->dp_subconnector_property)
1852 		mode_config->dp_subconnector_property =
1853 			drm_property_create_enum(connector->dev,
1854 				DRM_MODE_PROP_IMMUTABLE,
1855 				"subconnector",
1856 				drm_dp_subconnector_enum_list,
1857 				ARRAY_SIZE(drm_dp_subconnector_enum_list));
1858 
1859 	drm_object_attach_property(&connector->base,
1860 				   mode_config->dp_subconnector_property,
1861 				   DRM_MODE_SUBCONNECTOR_Unknown);
1862 }
1863 EXPORT_SYMBOL(drm_connector_attach_dp_subconnector_property);
1864 
1865 /**
1866  * DOC: HDMI connector properties
1867  *
1868  * Broadcast RGB (HDMI specific)
1869  *      Indicates the Quantization Range (Full vs Limited) used. The color
1870  *      processing pipeline will be adjusted to match the value of the
1871  *      property, and the Infoframes will be generated and sent accordingly.
1872  *
1873  *      This property is only relevant if the HDMI output format is RGB. If
1874  *      it's one of the YCbCr variant, it will be ignored.
1875  *
1876  *      The CRTC attached to the connector must be configured by user-space to
1877  *      always produce full-range pixels.
1878  *
1879  *      The value of this property can be one of the following:
1880  *
1881  *      Automatic:
1882  *              The quantization range is selected automatically based on the
1883  *              mode according to the HDMI specifications (HDMI 1.4b - Section
1884  *              6.6 - Video Quantization Ranges).
1885  *
1886  *      Full:
1887  *              Full quantization range is forced.
1888  *
1889  *      Limited 16:235:
1890  *              Limited quantization range is forced. Unlike the name suggests,
1891  *              this works for any number of bits-per-component.
1892  *
1893  *      Property values other than Automatic can result in colors being off (if
1894  *      limited is selected but the display expects full), or a black screen
1895  *      (if full is selected but the display expects limited).
1896  *
1897  *      Drivers can set up this property by calling
1898  *      drm_connector_attach_broadcast_rgb_property().
1899  *
1900  * content type (HDMI specific):
1901  *	Indicates content type setting to be used in HDMI infoframes to indicate
1902  *	content type for the external device, so that it adjusts its display
1903  *	settings accordingly.
1904  *
1905  *	The value of this property can be one of the following:
1906  *
1907  *	No Data:
1908  *		Content type is unknown
1909  *	Graphics:
1910  *		Content type is graphics
1911  *	Photo:
1912  *		Content type is photo
1913  *	Cinema:
1914  *		Content type is cinema
1915  *	Game:
1916  *		Content type is game
1917  *
1918  *	The meaning of each content type is defined in CTA-861-G table 15.
1919  *
1920  *	Drivers can set up this property by calling
1921  *	drm_connector_attach_content_type_property(). Decoding to
1922  *	infoframe values is done through drm_hdmi_avi_infoframe_content_type().
1923  */
1924 
1925 /*
1926  * TODO: Document the properties:
1927  *   - brightness
1928  *   - contrast
1929  *   - flicker reduction
1930  *   - hue
1931  *   - mode
1932  *   - overscan
1933  *   - saturation
1934  *   - select subconnector
1935  */
1936 /**
1937  * DOC: Analog TV Connector Properties
1938  *
1939  * TV Mode:
1940  *	Indicates the TV Mode used on an analog TV connector. The value
1941  *	of this property can be one of the following:
1942  *
1943  *	NTSC:
1944  *		TV Mode is CCIR System M (aka 525-lines) together with
1945  *		the NTSC Color Encoding.
1946  *
1947  *	NTSC-443:
1948  *
1949  *		TV Mode is CCIR System M (aka 525-lines) together with
1950  *		the NTSC Color Encoding, but with a color subcarrier
1951  *		frequency of 4.43MHz
1952  *
1953  *	NTSC-J:
1954  *
1955  *		TV Mode is CCIR System M (aka 525-lines) together with
1956  *		the NTSC Color Encoding, but with a black level equal to
1957  *		the blanking level.
1958  *
1959  *	PAL:
1960  *
1961  *		TV Mode is CCIR System B (aka 625-lines) together with
1962  *		the PAL Color Encoding.
1963  *
1964  *	PAL-M:
1965  *
1966  *		TV Mode is CCIR System M (aka 525-lines) together with
1967  *		the PAL Color Encoding.
1968  *
1969  *	PAL-N:
1970  *
1971  *		TV Mode is CCIR System N together with the PAL Color
1972  *		Encoding, a color subcarrier frequency of 3.58MHz, the
1973  *		SECAM color space, and narrower channels than other PAL
1974  *		variants.
1975  *
1976  *	SECAM:
1977  *
1978  *		TV Mode is CCIR System B (aka 625-lines) together with
1979  *		the SECAM Color Encoding.
1980  *
1981  *	Mono:
1982  *
1983  *		Use timings appropriate to the DRM mode, including
1984  *		equalizing pulses for a 525-line or 625-line mode,
1985  *		with no pedestal or color encoding.
1986  *
1987  *	Drivers can set up this property by calling
1988  *	drm_mode_create_tv_properties().
1989  */
1990 
1991 /**
1992  * drm_connector_attach_content_type_property - attach content-type property
1993  * @connector: connector to attach content type property on.
1994  *
1995  * Called by a driver the first time a HDMI connector is made.
1996  *
1997  * Returns: %0
1998  */
1999 int drm_connector_attach_content_type_property(struct drm_connector *connector)
2000 {
2001 	if (!drm_mode_create_content_type_property(connector->dev))
2002 		drm_object_attach_property(&connector->base,
2003 					   connector->dev->mode_config.content_type_property,
2004 					   DRM_MODE_CONTENT_TYPE_NO_DATA);
2005 	return 0;
2006 }
2007 EXPORT_SYMBOL(drm_connector_attach_content_type_property);
2008 
2009 /**
2010  * drm_connector_attach_tv_margin_properties - attach TV connector margin
2011  * 	properties
2012  * @connector: DRM connector
2013  *
2014  * Called by a driver when it needs to attach TV margin props to a connector.
2015  * Typically used on SDTV and HDMI connectors.
2016  */
2017 void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
2018 {
2019 	struct drm_device *dev = connector->dev;
2020 
2021 	drm_object_attach_property(&connector->base,
2022 				   dev->mode_config.tv_left_margin_property,
2023 				   0);
2024 	drm_object_attach_property(&connector->base,
2025 				   dev->mode_config.tv_right_margin_property,
2026 				   0);
2027 	drm_object_attach_property(&connector->base,
2028 				   dev->mode_config.tv_top_margin_property,
2029 				   0);
2030 	drm_object_attach_property(&connector->base,
2031 				   dev->mode_config.tv_bottom_margin_property,
2032 				   0);
2033 }
2034 EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
2035 
2036 /**
2037  * drm_mode_create_tv_margin_properties - create TV connector margin properties
2038  * @dev: DRM device
2039  *
2040  * Called by a driver's HDMI connector initialization routine, this function
2041  * creates the TV margin properties for a given device. No need to call this
2042  * function for an SDTV connector, it's already called from
2043  * drm_mode_create_tv_properties_legacy().
2044  *
2045  * Returns:
2046  * 0 on success or a negative error code on failure.
2047  */
2048 int drm_mode_create_tv_margin_properties(struct drm_device *dev)
2049 {
2050 	if (dev->mode_config.tv_left_margin_property)
2051 		return 0;
2052 
2053 	dev->mode_config.tv_left_margin_property =
2054 		drm_property_create_range(dev, 0, "left margin", 0, 100);
2055 	if (!dev->mode_config.tv_left_margin_property)
2056 		return -ENOMEM;
2057 
2058 	dev->mode_config.tv_right_margin_property =
2059 		drm_property_create_range(dev, 0, "right margin", 0, 100);
2060 	if (!dev->mode_config.tv_right_margin_property)
2061 		return -ENOMEM;
2062 
2063 	dev->mode_config.tv_top_margin_property =
2064 		drm_property_create_range(dev, 0, "top margin", 0, 100);
2065 	if (!dev->mode_config.tv_top_margin_property)
2066 		return -ENOMEM;
2067 
2068 	dev->mode_config.tv_bottom_margin_property =
2069 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
2070 	if (!dev->mode_config.tv_bottom_margin_property)
2071 		return -ENOMEM;
2072 
2073 	return 0;
2074 }
2075 EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
2076 
2077 /**
2078  * drm_mode_create_tv_properties_legacy - create TV specific connector properties
2079  * @dev: DRM device
2080  * @num_modes: number of different TV formats (modes) supported
2081  * @modes: array of pointers to strings containing name of each format
2082  *
2083  * Called by a driver's TV initialization routine, this function creates
2084  * the TV specific connector properties for a given device.  Caller is
2085  * responsible for allocating a list of format names and passing them to
2086  * this routine.
2087  *
2088  * NOTE: This functions registers the deprecated "mode" connector
2089  * property to select the analog TV mode (ie, NTSC, PAL, etc.). New
2090  * drivers must use drm_mode_create_tv_properties() instead.
2091  *
2092  * Returns:
2093  * 0 on success or a negative error code on failure.
2094  */
2095 int drm_mode_create_tv_properties_legacy(struct drm_device *dev,
2096 					 unsigned int num_modes,
2097 					 const char * const modes[])
2098 {
2099 	struct drm_property *tv_selector;
2100 	struct drm_property *tv_subconnector;
2101 	unsigned int i;
2102 
2103 	if (dev->mode_config.tv_select_subconnector_property)
2104 		return 0;
2105 
2106 	/*
2107 	 * Basic connector properties
2108 	 */
2109 	tv_selector = drm_property_create_enum(dev, 0,
2110 					  "select subconnector",
2111 					  drm_tv_select_enum_list,
2112 					  ARRAY_SIZE(drm_tv_select_enum_list));
2113 	if (!tv_selector)
2114 		goto nomem;
2115 
2116 	dev->mode_config.tv_select_subconnector_property = tv_selector;
2117 
2118 	tv_subconnector =
2119 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
2120 				    "subconnector",
2121 				    drm_tv_subconnector_enum_list,
2122 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
2123 	if (!tv_subconnector)
2124 		goto nomem;
2125 	dev->mode_config.tv_subconnector_property = tv_subconnector;
2126 
2127 	/*
2128 	 * Other, TV specific properties: margins & TV modes.
2129 	 */
2130 	if (drm_mode_create_tv_margin_properties(dev))
2131 		goto nomem;
2132 
2133 	if (num_modes) {
2134 		dev->mode_config.legacy_tv_mode_property =
2135 			drm_property_create(dev, DRM_MODE_PROP_ENUM,
2136 					    "mode", num_modes);
2137 		if (!dev->mode_config.legacy_tv_mode_property)
2138 			goto nomem;
2139 
2140 		for (i = 0; i < num_modes; i++)
2141 			drm_property_add_enum(dev->mode_config.legacy_tv_mode_property,
2142 					      i, modes[i]);
2143 	}
2144 
2145 	dev->mode_config.tv_brightness_property =
2146 		drm_property_create_range(dev, 0, "brightness", 0, 100);
2147 	if (!dev->mode_config.tv_brightness_property)
2148 		goto nomem;
2149 
2150 	dev->mode_config.tv_contrast_property =
2151 		drm_property_create_range(dev, 0, "contrast", 0, 100);
2152 	if (!dev->mode_config.tv_contrast_property)
2153 		goto nomem;
2154 
2155 	dev->mode_config.tv_flicker_reduction_property =
2156 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
2157 	if (!dev->mode_config.tv_flicker_reduction_property)
2158 		goto nomem;
2159 
2160 	dev->mode_config.tv_overscan_property =
2161 		drm_property_create_range(dev, 0, "overscan", 0, 100);
2162 	if (!dev->mode_config.tv_overscan_property)
2163 		goto nomem;
2164 
2165 	dev->mode_config.tv_saturation_property =
2166 		drm_property_create_range(dev, 0, "saturation", 0, 100);
2167 	if (!dev->mode_config.tv_saturation_property)
2168 		goto nomem;
2169 
2170 	dev->mode_config.tv_hue_property =
2171 		drm_property_create_range(dev, 0, "hue", 0, 100);
2172 	if (!dev->mode_config.tv_hue_property)
2173 		goto nomem;
2174 
2175 	return 0;
2176 nomem:
2177 	return -ENOMEM;
2178 }
2179 EXPORT_SYMBOL(drm_mode_create_tv_properties_legacy);
2180 
2181 /**
2182  * drm_mode_create_tv_properties - create TV specific connector properties
2183  * @dev: DRM device
2184  * @supported_tv_modes: Bitmask of TV modes supported (See DRM_MODE_TV_MODE_*)
2185  *
2186  * Called by a driver's TV initialization routine, this function creates
2187  * the TV specific connector properties for a given device.
2188  *
2189  * Returns:
2190  * 0 on success or a negative error code on failure.
2191  */
2192 int drm_mode_create_tv_properties(struct drm_device *dev,
2193 				  unsigned int supported_tv_modes)
2194 {
2195 	struct drm_prop_enum_list tv_mode_list[DRM_MODE_TV_MODE_MAX];
2196 	struct drm_property *tv_mode;
2197 	unsigned int i, len = 0;
2198 
2199 	if (dev->mode_config.tv_mode_property)
2200 		return 0;
2201 
2202 	for (i = 0; i < DRM_MODE_TV_MODE_MAX; i++) {
2203 		if (!(supported_tv_modes & BIT(i)))
2204 			continue;
2205 
2206 		tv_mode_list[len].type = i;
2207 		tv_mode_list[len].name = drm_get_tv_mode_name(i);
2208 		len++;
2209 	}
2210 
2211 	tv_mode = drm_property_create_enum(dev, 0, "TV mode",
2212 					   tv_mode_list, len);
2213 	if (!tv_mode)
2214 		return -ENOMEM;
2215 
2216 	dev->mode_config.tv_mode_property = tv_mode;
2217 
2218 	return drm_mode_create_tv_properties_legacy(dev, 0, NULL);
2219 }
2220 EXPORT_SYMBOL(drm_mode_create_tv_properties);
2221 
2222 /**
2223  * drm_mode_create_scaling_mode_property - create scaling mode property
2224  * @dev: DRM device
2225  *
2226  * Called by a driver the first time it's needed, must be attached to desired
2227  * connectors.
2228  *
2229  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
2230  * instead to correctly assign &drm_connector_state.scaling_mode
2231  * in the atomic state.
2232  *
2233  * Returns: %0
2234  */
2235 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
2236 {
2237 	struct drm_property *scaling_mode;
2238 
2239 	if (dev->mode_config.scaling_mode_property)
2240 		return 0;
2241 
2242 	scaling_mode =
2243 		drm_property_create_enum(dev, 0, "scaling mode",
2244 				drm_scaling_mode_enum_list,
2245 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
2246 
2247 	dev->mode_config.scaling_mode_property = scaling_mode;
2248 
2249 	return 0;
2250 }
2251 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
2252 
2253 /**
2254  * DOC: Variable refresh properties
2255  *
2256  * Variable refresh rate capable displays can dynamically adjust their
2257  * refresh rate by extending the duration of their vertical front porch
2258  * until page flip or timeout occurs. This can reduce or remove stuttering
2259  * and latency in scenarios where the page flip does not align with the
2260  * vblank interval.
2261  *
2262  * An example scenario would be an application flipping at a constant rate
2263  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
2264  * interval and the same contents will be displayed twice. This can be
2265  * observed as stuttering for content with motion.
2266  *
2267  * If variable refresh rate was active on a display that supported a
2268  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
2269  * for the example scenario. The minimum supported variable refresh rate of
2270  * 35Hz is below the page flip frequency and the vertical front porch can
2271  * be extended until the page flip occurs. The vblank interval will be
2272  * directly aligned to the page flip rate.
2273  *
2274  * Not all userspace content is suitable for use with variable refresh rate.
2275  * Large and frequent changes in vertical front porch duration may worsen
2276  * perceived stuttering for input sensitive applications.
2277  *
2278  * Panel brightness will also vary with vertical front porch duration. Some
2279  * panels may have noticeable differences in brightness between the minimum
2280  * vertical front porch duration and the maximum vertical front porch duration.
2281  * Large and frequent changes in vertical front porch duration may produce
2282  * observable flickering for such panels.
2283  *
2284  * Userspace control for variable refresh rate is supported via properties
2285  * on the &drm_connector and &drm_crtc objects.
2286  *
2287  * "vrr_capable":
2288  *	Optional &drm_connector boolean property that drivers should attach
2289  *	with drm_connector_attach_vrr_capable_property() on connectors that
2290  *	could support variable refresh rates. Drivers should update the
2291  *	property value by calling drm_connector_set_vrr_capable_property().
2292  *
2293  *	Absence of the property should indicate absence of support.
2294  *
2295  * "VRR_ENABLED":
2296  *	Default &drm_crtc boolean property that notifies the driver that the
2297  *	content on the CRTC is suitable for variable refresh rate presentation.
2298  *	The driver will take this property as a hint to enable variable
2299  *	refresh rate support if the receiver supports it, ie. if the
2300  *	"vrr_capable" property is true on the &drm_connector object. The
2301  *	vertical front porch duration will be extended until page-flip or
2302  *	timeout when enabled.
2303  *
2304  *	The minimum vertical front porch duration is defined as the vertical
2305  *	front porch duration for the current mode.
2306  *
2307  *	The maximum vertical front porch duration is greater than or equal to
2308  *	the minimum vertical front porch duration. The duration is derived
2309  *	from the minimum supported variable refresh rate for the connector.
2310  *
2311  *	The driver may place further restrictions within these minimum
2312  *	and maximum bounds.
2313  */
2314 
2315 /**
2316  * drm_connector_attach_vrr_capable_property - creates the
2317  * vrr_capable property
2318  * @connector: connector to create the vrr_capable property on.
2319  *
2320  * This is used by atomic drivers to add support for querying
2321  * variable refresh rate capability for a connector.
2322  *
2323  * Returns:
2324  * Zero on success, negative errno on failure.
2325  */
2326 int drm_connector_attach_vrr_capable_property(
2327 	struct drm_connector *connector)
2328 {
2329 	struct drm_device *dev = connector->dev;
2330 	struct drm_property *prop;
2331 
2332 	if (!connector->vrr_capable_property) {
2333 		prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
2334 			"vrr_capable");
2335 		if (!prop)
2336 			return -ENOMEM;
2337 
2338 		connector->vrr_capable_property = prop;
2339 		drm_object_attach_property(&connector->base, prop, 0);
2340 	}
2341 
2342 	return 0;
2343 }
2344 EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
2345 
2346 /**
2347  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
2348  * @connector: connector to attach scaling mode property on.
2349  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
2350  *
2351  * This is used to add support for scaling mode to atomic drivers.
2352  * The scaling mode will be set to &drm_connector_state.scaling_mode
2353  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
2354  *
2355  * This is the atomic version of drm_mode_create_scaling_mode_property().
2356  *
2357  * Returns:
2358  * Zero on success, negative errno on failure.
2359  */
2360 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
2361 					       u32 scaling_mode_mask)
2362 {
2363 	struct drm_device *dev = connector->dev;
2364 	struct drm_property *scaling_mode_property;
2365 	int i;
2366 	const unsigned valid_scaling_mode_mask =
2367 		(1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
2368 
2369 	if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
2370 		    scaling_mode_mask & ~valid_scaling_mode_mask))
2371 		return -EINVAL;
2372 
2373 	scaling_mode_property =
2374 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
2375 				    hweight32(scaling_mode_mask));
2376 
2377 	if (!scaling_mode_property)
2378 		return -ENOMEM;
2379 
2380 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
2381 		int ret;
2382 
2383 		if (!(BIT(i) & scaling_mode_mask))
2384 			continue;
2385 
2386 		ret = drm_property_add_enum(scaling_mode_property,
2387 					    drm_scaling_mode_enum_list[i].type,
2388 					    drm_scaling_mode_enum_list[i].name);
2389 
2390 		if (ret) {
2391 			drm_property_destroy(dev, scaling_mode_property);
2392 
2393 			return ret;
2394 		}
2395 	}
2396 
2397 	drm_object_attach_property(&connector->base,
2398 				   scaling_mode_property, 0);
2399 
2400 	connector->scaling_mode_property = scaling_mode_property;
2401 
2402 	return 0;
2403 }
2404 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
2405 
2406 /**
2407  * drm_mode_create_aspect_ratio_property - create aspect ratio property
2408  * @dev: DRM device
2409  *
2410  * Called by a driver the first time it's needed, must be attached to desired
2411  * connectors.
2412  *
2413  * Returns:
2414  * Zero on success, negative errno on failure.
2415  */
2416 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
2417 {
2418 	if (dev->mode_config.aspect_ratio_property)
2419 		return 0;
2420 
2421 	dev->mode_config.aspect_ratio_property =
2422 		drm_property_create_enum(dev, 0, "aspect ratio",
2423 				drm_aspect_ratio_enum_list,
2424 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
2425 
2426 	if (dev->mode_config.aspect_ratio_property == NULL)
2427 		return -ENOMEM;
2428 
2429 	return 0;
2430 }
2431 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
2432 
2433 /**
2434  * DOC: standard connector properties
2435  *
2436  * Colorspace:
2437  *	This property is used to inform the driver about the color encoding
2438  *	user space configured the pixel operation properties to produce.
2439  *	The variants set the colorimetry, transfer characteristics, and which
2440  *	YCbCr conversion should be used when necessary.
2441  *	The transfer characteristics from HDR_OUTPUT_METADATA takes precedence
2442  *	over this property.
2443  *	User space always configures the pixel operation properties to produce
2444  *	full quantization range data (see the Broadcast RGB property).
2445  *
2446  *	Drivers inform the sink about what colorimetry, transfer
2447  *	characteristics, YCbCr conversion, and quantization range to expect
2448  *	(this can depend on the output mode, output format and other
2449  *	properties). Drivers also convert the user space provided data to what
2450  *	the sink expects.
2451  *
2452  *	User space has to check if the sink supports all of the possible
2453  *	colorimetries that the driver is allowed to pick by parsing the EDID.
2454  *
2455  *	For historical reasons this property exposes a number of variants which
2456  *	result in undefined behavior.
2457  *
2458  *	Default:
2459  *		The behavior is driver-specific.
2460  *
2461  *	BT2020_RGB:
2462  *
2463  *	BT2020_YCC:
2464  *		User space configures the pixel operation properties to produce
2465  *		RGB content with Rec. ITU-R BT.2020 colorimetry, Rec.
2466  *		ITU-R BT.2020 (Table 4, RGB) transfer characteristics and full
2467  *		quantization range.
2468  *		User space can use the HDR_OUTPUT_METADATA property to set the
2469  *		transfer characteristics to PQ (Rec. ITU-R BT.2100 Table 4) or
2470  *		HLG (Rec. ITU-R BT.2100 Table 5) in which case, user space
2471  *		configures pixel operation properties to produce content with
2472  *		the respective transfer characteristics.
2473  *		User space has to make sure the sink supports Rec.
2474  *		ITU-R BT.2020 R'G'B' and Rec. ITU-R BT.2020 Y'C'BC'R
2475  *		colorimetry.
2476  *		Drivers can configure the sink to use an RGB format, tell the
2477  *		sink to expect Rec. ITU-R BT.2020 R'G'B' colorimetry and convert
2478  *		to the appropriate quantization range.
2479  *		Drivers can configure the sink to use a YCbCr format, tell the
2480  *		sink to expect Rec. ITU-R BT.2020 Y'C'BC'R colorimetry, convert
2481  *		to YCbCr using the Rec. ITU-R BT.2020 non-constant luminance
2482  *		conversion matrix and convert to the appropriate quantization
2483  *		range.
2484  *		The variants BT2020_RGB and BT2020_YCC are equivalent and the
2485  *		driver chooses between RGB and YCbCr on its own.
2486  *
2487  *	SMPTE_170M_YCC:
2488  *	BT709_YCC:
2489  *	XVYCC_601:
2490  *	XVYCC_709:
2491  *	SYCC_601:
2492  *	opYCC_601:
2493  *	opRGB:
2494  *	BT2020_CYCC:
2495  *	DCI-P3_RGB_D65:
2496  *	DCI-P3_RGB_Theater:
2497  *	RGB_WIDE_FIXED:
2498  *	RGB_WIDE_FLOAT:
2499  *
2500  *	BT601_YCC:
2501  *		The behavior is undefined.
2502  *
2503  * Because between HDMI and DP have different colorspaces,
2504  * drm_mode_create_hdmi_colorspace_property() is used for HDMI connector and
2505  * drm_mode_create_dp_colorspace_property() is used for DP connector.
2506  */
2507 
2508 static int drm_mode_create_colorspace_property(struct drm_connector *connector,
2509 					u32 supported_colorspaces)
2510 {
2511 	struct drm_device *dev = connector->dev;
2512 	u32 colorspaces = supported_colorspaces | BIT(DRM_MODE_COLORIMETRY_DEFAULT);
2513 	struct drm_prop_enum_list enum_list[DRM_MODE_COLORIMETRY_COUNT];
2514 	int i, len;
2515 
2516 	if (connector->colorspace_property)
2517 		return 0;
2518 
2519 	if (!supported_colorspaces) {
2520 		drm_err(dev, "No supported colorspaces provded on [CONNECTOR:%d:%s]\n",
2521 			    connector->base.id, connector->name);
2522 		return -EINVAL;
2523 	}
2524 
2525 	if ((supported_colorspaces & -BIT(DRM_MODE_COLORIMETRY_COUNT)) != 0) {
2526 		drm_err(dev, "Unknown colorspace provded on [CONNECTOR:%d:%s]\n",
2527 			    connector->base.id, connector->name);
2528 		return -EINVAL;
2529 	}
2530 
2531 	len = 0;
2532 	for (i = 0; i < DRM_MODE_COLORIMETRY_COUNT; i++) {
2533 		if ((colorspaces & BIT(i)) == 0)
2534 			continue;
2535 
2536 		enum_list[len].type = i;
2537 		enum_list[len].name = colorspace_names[i];
2538 		len++;
2539 	}
2540 
2541 	connector->colorspace_property =
2542 		drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "Colorspace",
2543 					enum_list,
2544 					len);
2545 
2546 	if (!connector->colorspace_property)
2547 		return -ENOMEM;
2548 
2549 	return 0;
2550 }
2551 
2552 /**
2553  * drm_mode_create_hdmi_colorspace_property - create hdmi colorspace property
2554  * @connector: connector to create the Colorspace property on.
2555  * @supported_colorspaces: bitmap of supported color spaces
2556  *
2557  * Called by a driver the first time it's needed, must be attached to desired
2558  * HDMI connectors.
2559  *
2560  * Returns:
2561  * Zero on success, negative errno on failure.
2562  */
2563 int drm_mode_create_hdmi_colorspace_property(struct drm_connector *connector,
2564 					     u32 supported_colorspaces)
2565 {
2566 	u32 colorspaces;
2567 
2568 	if (supported_colorspaces)
2569 		colorspaces = supported_colorspaces & hdmi_colorspaces;
2570 	else
2571 		colorspaces = hdmi_colorspaces;
2572 
2573 	return drm_mode_create_colorspace_property(connector, colorspaces);
2574 }
2575 EXPORT_SYMBOL(drm_mode_create_hdmi_colorspace_property);
2576 
2577 /**
2578  * drm_mode_create_dp_colorspace_property - create dp colorspace property
2579  * @connector: connector to create the Colorspace property on.
2580  * @supported_colorspaces: bitmap of supported color spaces
2581  *
2582  * Called by a driver the first time it's needed, must be attached to desired
2583  * DP connectors.
2584  *
2585  * Returns:
2586  * Zero on success, negative errno on failure.
2587  */
2588 int drm_mode_create_dp_colorspace_property(struct drm_connector *connector,
2589 					   u32 supported_colorspaces)
2590 {
2591 	u32 colorspaces;
2592 
2593 	if (supported_colorspaces)
2594 		colorspaces = supported_colorspaces & dp_colorspaces;
2595 	else
2596 		colorspaces = dp_colorspaces;
2597 
2598 	return drm_mode_create_colorspace_property(connector, colorspaces);
2599 }
2600 EXPORT_SYMBOL(drm_mode_create_dp_colorspace_property);
2601 
2602 /**
2603  * drm_mode_create_content_type_property - create content type property
2604  * @dev: DRM device
2605  *
2606  * Called by a driver the first time it's needed, must be attached to desired
2607  * connectors.
2608  *
2609  * Returns:
2610  * Zero on success, negative errno on failure.
2611  */
2612 int drm_mode_create_content_type_property(struct drm_device *dev)
2613 {
2614 	if (dev->mode_config.content_type_property)
2615 		return 0;
2616 
2617 	dev->mode_config.content_type_property =
2618 		drm_property_create_enum(dev, 0, "content type",
2619 					 drm_content_type_enum_list,
2620 					 ARRAY_SIZE(drm_content_type_enum_list));
2621 
2622 	if (dev->mode_config.content_type_property == NULL)
2623 		return -ENOMEM;
2624 
2625 	return 0;
2626 }
2627 EXPORT_SYMBOL(drm_mode_create_content_type_property);
2628 
2629 /**
2630  * drm_mode_create_suggested_offset_properties - create suggests offset properties
2631  * @dev: DRM device
2632  *
2633  * Create the suggested x/y offset property for connectors.
2634  *
2635  * Returns:
2636  * 0 on success or a negative error code on failure.
2637  */
2638 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
2639 {
2640 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
2641 		return 0;
2642 
2643 	dev->mode_config.suggested_x_property =
2644 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
2645 
2646 	dev->mode_config.suggested_y_property =
2647 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
2648 
2649 	if (dev->mode_config.suggested_x_property == NULL ||
2650 	    dev->mode_config.suggested_y_property == NULL)
2651 		return -ENOMEM;
2652 	return 0;
2653 }
2654 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
2655 
2656 /**
2657  * drm_connector_set_path_property - set tile property on connector
2658  * @connector: connector to set property on.
2659  * @path: path to use for property; must not be NULL.
2660  *
2661  * This creates a property to expose to userspace to specify a
2662  * connector path. This is mainly used for DisplayPort MST where
2663  * connectors have a topology and we want to allow userspace to give
2664  * them more meaningful names.
2665  *
2666  * Returns:
2667  * Zero on success, negative errno on failure.
2668  */
2669 int drm_connector_set_path_property(struct drm_connector *connector,
2670 				    const char *path)
2671 {
2672 	struct drm_device *dev = connector->dev;
2673 	int ret;
2674 
2675 	ret = drm_property_replace_global_blob(dev,
2676 					       &connector->path_blob_ptr,
2677 					       strlen(path) + 1,
2678 					       path,
2679 					       &connector->base,
2680 					       dev->mode_config.path_property);
2681 	return ret;
2682 }
2683 EXPORT_SYMBOL(drm_connector_set_path_property);
2684 
2685 /**
2686  * drm_connector_set_tile_property - set tile property on connector
2687  * @connector: connector to set property on.
2688  *
2689  * This looks up the tile information for a connector, and creates a
2690  * property for userspace to parse if it exists. The property is of
2691  * the form of 8 integers using ':' as a separator.
2692  * This is used for dual port tiled displays with DisplayPort SST
2693  * or DisplayPort MST connectors.
2694  *
2695  * Returns:
2696  * Zero on success, errno on failure.
2697  */
2698 int drm_connector_set_tile_property(struct drm_connector *connector)
2699 {
2700 	struct drm_device *dev = connector->dev;
2701 	char tile[256];
2702 	int ret;
2703 
2704 	if (!connector->has_tile) {
2705 		ret  = drm_property_replace_global_blob(dev,
2706 							&connector->tile_blob_ptr,
2707 							0,
2708 							NULL,
2709 							&connector->base,
2710 							dev->mode_config.tile_property);
2711 		return ret;
2712 	}
2713 
2714 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
2715 		 connector->tile_group->id, connector->tile_is_single_monitor,
2716 		 connector->num_h_tile, connector->num_v_tile,
2717 		 connector->tile_h_loc, connector->tile_v_loc,
2718 		 connector->tile_h_size, connector->tile_v_size);
2719 
2720 	ret = drm_property_replace_global_blob(dev,
2721 					       &connector->tile_blob_ptr,
2722 					       strlen(tile) + 1,
2723 					       tile,
2724 					       &connector->base,
2725 					       dev->mode_config.tile_property);
2726 	return ret;
2727 }
2728 EXPORT_SYMBOL(drm_connector_set_tile_property);
2729 
2730 /**
2731  * drm_connector_set_link_status_property - Set link status property of a connector
2732  * @connector: drm connector
2733  * @link_status: new value of link status property (0: Good, 1: Bad)
2734  *
2735  * In usual working scenario, this link status property will always be set to
2736  * "GOOD". If something fails during or after a mode set, the kernel driver
2737  * may set this link status property to "BAD". The caller then needs to send a
2738  * hotplug uevent for userspace to re-check the valid modes through
2739  * GET_CONNECTOR_IOCTL and retry modeset.
2740  *
2741  * Note: Drivers cannot rely on userspace to support this property and
2742  * issue a modeset. As such, they may choose to handle issues (like
2743  * re-training a link) without userspace's intervention.
2744  *
2745  * The reason for adding this property is to handle link training failures, but
2746  * it is not limited to DP or link training. For example, if we implement
2747  * asynchronous setcrtc, this property can be used to report any failures in that.
2748  */
2749 void drm_connector_set_link_status_property(struct drm_connector *connector,
2750 					    uint64_t link_status)
2751 {
2752 	struct drm_device *dev = connector->dev;
2753 
2754 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2755 	connector->state->link_status = link_status;
2756 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2757 }
2758 EXPORT_SYMBOL(drm_connector_set_link_status_property);
2759 
2760 /**
2761  * drm_connector_attach_max_bpc_property - attach "max bpc" property
2762  * @connector: connector to attach max bpc property on.
2763  * @min: The minimum bit depth supported by the connector.
2764  * @max: The maximum bit depth supported by the connector.
2765  *
2766  * This is used to add support for limiting the bit depth on a connector.
2767  *
2768  * Returns:
2769  * Zero on success, negative errno on failure.
2770  */
2771 int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
2772 					  int min, int max)
2773 {
2774 	struct drm_device *dev = connector->dev;
2775 	struct drm_property *prop;
2776 
2777 	prop = connector->max_bpc_property;
2778 	if (!prop) {
2779 		prop = drm_property_create_range(dev, 0, "max bpc", min, max);
2780 		if (!prop)
2781 			return -ENOMEM;
2782 
2783 		connector->max_bpc_property = prop;
2784 	}
2785 
2786 	drm_object_attach_property(&connector->base, prop, max);
2787 	connector->state->max_requested_bpc = max;
2788 	connector->state->max_bpc = max;
2789 
2790 	return 0;
2791 }
2792 EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
2793 
2794 /**
2795  * drm_connector_attach_hdr_output_metadata_property - attach "HDR_OUTPUT_METADA" property
2796  * @connector: connector to attach the property on.
2797  *
2798  * This is used to allow the userspace to send HDR Metadata to the
2799  * driver.
2800  *
2801  * Returns:
2802  * Zero on success, negative errno on failure.
2803  */
2804 int drm_connector_attach_hdr_output_metadata_property(struct drm_connector *connector)
2805 {
2806 	struct drm_device *dev = connector->dev;
2807 	struct drm_property *prop = dev->mode_config.hdr_output_metadata_property;
2808 
2809 	drm_object_attach_property(&connector->base, prop, 0);
2810 
2811 	return 0;
2812 }
2813 EXPORT_SYMBOL(drm_connector_attach_hdr_output_metadata_property);
2814 
2815 /**
2816  * drm_connector_attach_broadcast_rgb_property - attach "Broadcast RGB" property
2817  * @connector: connector to attach the property on.
2818  *
2819  * This is used to add support for forcing the RGB range on a connector
2820  *
2821  * Returns:
2822  * Zero on success, negative errno on failure.
2823  */
2824 int drm_connector_attach_broadcast_rgb_property(struct drm_connector *connector)
2825 {
2826 	struct drm_device *dev = connector->dev;
2827 	struct drm_property *prop;
2828 
2829 	prop = connector->broadcast_rgb_property;
2830 	if (!prop) {
2831 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
2832 						"Broadcast RGB",
2833 						broadcast_rgb_names,
2834 						ARRAY_SIZE(broadcast_rgb_names));
2835 		if (!prop)
2836 			return -EINVAL;
2837 
2838 		connector->broadcast_rgb_property = prop;
2839 	}
2840 
2841 	drm_object_attach_property(&connector->base, prop,
2842 				   DRM_HDMI_BROADCAST_RGB_AUTO);
2843 
2844 	return 0;
2845 }
2846 EXPORT_SYMBOL(drm_connector_attach_broadcast_rgb_property);
2847 
2848 /**
2849  * drm_connector_attach_colorspace_property - attach "Colorspace" property
2850  * @connector: connector to attach the property on.
2851  *
2852  * This is used to allow the userspace to signal the output colorspace
2853  * to the driver.
2854  *
2855  * Returns:
2856  * Zero on success, negative errno on failure.
2857  */
2858 int drm_connector_attach_colorspace_property(struct drm_connector *connector)
2859 {
2860 	struct drm_property *prop = connector->colorspace_property;
2861 
2862 	drm_object_attach_property(&connector->base, prop, DRM_MODE_COLORIMETRY_DEFAULT);
2863 
2864 	return 0;
2865 }
2866 EXPORT_SYMBOL(drm_connector_attach_colorspace_property);
2867 
2868 /**
2869  * drm_connector_atomic_hdr_metadata_equal - checks if the hdr metadata changed
2870  * @old_state: old connector state to compare
2871  * @new_state: new connector state to compare
2872  *
2873  * This is used by HDR-enabled drivers to test whether the HDR metadata
2874  * have changed between two different connector state (and thus probably
2875  * requires a full blown mode change).
2876  *
2877  * Returns:
2878  * True if the metadata are equal, False otherwise
2879  */
2880 bool drm_connector_atomic_hdr_metadata_equal(struct drm_connector_state *old_state,
2881 					     struct drm_connector_state *new_state)
2882 {
2883 	struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
2884 	struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
2885 
2886 	if (!old_blob || !new_blob)
2887 		return old_blob == new_blob;
2888 
2889 	if (old_blob->length != new_blob->length)
2890 		return false;
2891 
2892 	return !memcmp(old_blob->data, new_blob->data, old_blob->length);
2893 }
2894 EXPORT_SYMBOL(drm_connector_atomic_hdr_metadata_equal);
2895 
2896 /**
2897  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
2898  * capable property for a connector
2899  * @connector: drm connector
2900  * @capable: True if the connector is variable refresh rate capable
2901  *
2902  * Should be used by atomic drivers to update the indicated support for
2903  * variable refresh rate over a connector.
2904  */
2905 void drm_connector_set_vrr_capable_property(
2906 		struct drm_connector *connector, bool capable)
2907 {
2908 	if (!connector->vrr_capable_property)
2909 		return;
2910 
2911 	drm_object_property_set_value(&connector->base,
2912 				      connector->vrr_capable_property,
2913 				      capable);
2914 }
2915 EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
2916 
2917 /**
2918  * drm_connector_set_panel_orientation - sets the connector's panel_orientation
2919  * @connector: connector for which to set the panel-orientation property.
2920  * @panel_orientation: drm_panel_orientation value to set
2921  *
2922  * This function sets the connector's panel_orientation and attaches
2923  * a "panel orientation" property to the connector.
2924  *
2925  * Calling this function on a connector where the panel_orientation has
2926  * already been set is a no-op (e.g. the orientation has been overridden with
2927  * a kernel commandline option).
2928  *
2929  * It is allowed to call this function with a panel_orientation of
2930  * DRM_MODE_PANEL_ORIENTATION_UNKNOWN, in which case it is a no-op.
2931  *
2932  * The function shouldn't be called in panel after drm is registered (i.e.
2933  * drm_dev_register() is called in drm).
2934  *
2935  * Returns:
2936  * Zero on success, negative errno on failure.
2937  */
2938 int drm_connector_set_panel_orientation(
2939 	struct drm_connector *connector,
2940 	enum drm_panel_orientation panel_orientation)
2941 {
2942 	struct drm_device *dev = connector->dev;
2943 	struct drm_display_info *info = &connector->display_info;
2944 	struct drm_property *prop;
2945 
2946 	/* Already set? */
2947 	if (info->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2948 		return 0;
2949 
2950 	/* Don't attach the property if the orientation is unknown */
2951 	if (panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2952 		return 0;
2953 
2954 	info->panel_orientation = panel_orientation;
2955 
2956 	prop = dev->mode_config.panel_orientation_property;
2957 	if (!prop) {
2958 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
2959 				"panel orientation",
2960 				drm_panel_orientation_enum_list,
2961 				ARRAY_SIZE(drm_panel_orientation_enum_list));
2962 		if (!prop)
2963 			return -ENOMEM;
2964 
2965 		dev->mode_config.panel_orientation_property = prop;
2966 	}
2967 
2968 	drm_object_attach_property(&connector->base, prop,
2969 				   info->panel_orientation);
2970 	return 0;
2971 }
2972 EXPORT_SYMBOL(drm_connector_set_panel_orientation);
2973 
2974 /**
2975  * drm_connector_set_panel_orientation_with_quirk - set the
2976  *	connector's panel_orientation after checking for quirks
2977  * @connector: connector for which to init the panel-orientation property.
2978  * @panel_orientation: drm_panel_orientation value to set
2979  * @width: width in pixels of the panel, used for panel quirk detection
2980  * @height: height in pixels of the panel, used for panel quirk detection
2981  *
2982  * Like drm_connector_set_panel_orientation(), but with a check for platform
2983  * specific (e.g. DMI based) quirks overriding the passed in panel_orientation.
2984  *
2985  * Returns:
2986  * Zero on success, negative errno on failure.
2987  */
2988 int drm_connector_set_panel_orientation_with_quirk(
2989 	struct drm_connector *connector,
2990 	enum drm_panel_orientation panel_orientation,
2991 	int width, int height)
2992 {
2993 	int orientation_quirk;
2994 
2995 	orientation_quirk = drm_get_panel_orientation_quirk(width, height);
2996 	if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2997 		panel_orientation = orientation_quirk;
2998 
2999 	return drm_connector_set_panel_orientation(connector,
3000 						   panel_orientation);
3001 }
3002 EXPORT_SYMBOL(drm_connector_set_panel_orientation_with_quirk);
3003 
3004 /**
3005  * drm_connector_set_orientation_from_panel -
3006  *	set the connector's panel_orientation from panel's callback.
3007  * @connector: connector for which to init the panel-orientation property.
3008  * @panel: panel that can provide orientation information.
3009  *
3010  * Drm drivers should call this function before drm_dev_register().
3011  * Orientation is obtained from panel's .get_orientation() callback.
3012  *
3013  * Returns:
3014  * Zero on success, negative errno on failure.
3015  */
3016 int drm_connector_set_orientation_from_panel(
3017 	struct drm_connector *connector,
3018 	struct drm_panel *panel)
3019 {
3020 	enum drm_panel_orientation orientation;
3021 
3022 	if (panel && panel->funcs && panel->funcs->get_orientation)
3023 		orientation = panel->funcs->get_orientation(panel);
3024 	else
3025 		orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
3026 
3027 	return drm_connector_set_panel_orientation(connector, orientation);
3028 }
3029 EXPORT_SYMBOL(drm_connector_set_orientation_from_panel);
3030 
3031 static const struct drm_prop_enum_list privacy_screen_enum[] = {
3032 	{ PRIVACY_SCREEN_DISABLED,		"Disabled" },
3033 	{ PRIVACY_SCREEN_ENABLED,		"Enabled" },
3034 	{ PRIVACY_SCREEN_DISABLED_LOCKED,	"Disabled-locked" },
3035 	{ PRIVACY_SCREEN_ENABLED_LOCKED,	"Enabled-locked" },
3036 };
3037 
3038 /**
3039  * drm_connector_create_privacy_screen_properties - create the drm connecter's
3040  *    privacy-screen properties.
3041  * @connector: connector for which to create the privacy-screen properties
3042  *
3043  * This function creates the "privacy-screen sw-state" and "privacy-screen
3044  * hw-state" properties for the connector. They are not attached.
3045  */
3046 void
3047 drm_connector_create_privacy_screen_properties(struct drm_connector *connector)
3048 {
3049 	if (connector->privacy_screen_sw_state_property)
3050 		return;
3051 
3052 	/* Note sw-state only supports the first 2 values of the enum */
3053 	connector->privacy_screen_sw_state_property =
3054 		drm_property_create_enum(connector->dev, DRM_MODE_PROP_ENUM,
3055 				"privacy-screen sw-state",
3056 				privacy_screen_enum, 2);
3057 
3058 	connector->privacy_screen_hw_state_property =
3059 		drm_property_create_enum(connector->dev,
3060 				DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_ENUM,
3061 				"privacy-screen hw-state",
3062 				privacy_screen_enum,
3063 				ARRAY_SIZE(privacy_screen_enum));
3064 }
3065 EXPORT_SYMBOL(drm_connector_create_privacy_screen_properties);
3066 
3067 /**
3068  * drm_connector_attach_privacy_screen_properties - attach the drm connecter's
3069  *    privacy-screen properties.
3070  * @connector: connector on which to attach the privacy-screen properties
3071  *
3072  * This function attaches the "privacy-screen sw-state" and "privacy-screen
3073  * hw-state" properties to the connector. The initial state of both is set
3074  * to "Disabled".
3075  */
3076 void
3077 drm_connector_attach_privacy_screen_properties(struct drm_connector *connector)
3078 {
3079 	if (!connector->privacy_screen_sw_state_property)
3080 		return;
3081 
3082 	drm_object_attach_property(&connector->base,
3083 				   connector->privacy_screen_sw_state_property,
3084 				   PRIVACY_SCREEN_DISABLED);
3085 
3086 	drm_object_attach_property(&connector->base,
3087 				   connector->privacy_screen_hw_state_property,
3088 				   PRIVACY_SCREEN_DISABLED);
3089 }
3090 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_properties);
3091 
3092 static void drm_connector_update_privacy_screen_properties(
3093 	struct drm_connector *connector, bool set_sw_state)
3094 {
3095 	enum drm_privacy_screen_status sw_state, hw_state;
3096 
3097 	drm_privacy_screen_get_state(connector->privacy_screen,
3098 				     &sw_state, &hw_state);
3099 
3100 	if (set_sw_state)
3101 		connector->state->privacy_screen_sw_state = sw_state;
3102 	drm_object_property_set_value(&connector->base,
3103 			connector->privacy_screen_hw_state_property, hw_state);
3104 }
3105 
3106 static int drm_connector_privacy_screen_notifier(
3107 	struct notifier_block *nb, unsigned long action, void *data)
3108 {
3109 	struct drm_connector *connector =
3110 		container_of(nb, struct drm_connector, privacy_screen_notifier);
3111 	struct drm_device *dev = connector->dev;
3112 
3113 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
3114 	drm_connector_update_privacy_screen_properties(connector, true);
3115 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
3116 
3117 	drm_sysfs_connector_property_event(connector,
3118 					   connector->privacy_screen_sw_state_property);
3119 	drm_sysfs_connector_property_event(connector,
3120 					   connector->privacy_screen_hw_state_property);
3121 
3122 	return NOTIFY_DONE;
3123 }
3124 
3125 /**
3126  * drm_connector_attach_privacy_screen_provider - attach a privacy-screen to
3127  *    the connector
3128  * @connector: connector to attach the privacy-screen to
3129  * @priv: drm_privacy_screen to attach
3130  *
3131  * Create and attach the standard privacy-screen properties and register
3132  * a generic notifier for generating sysfs-connector-status-events
3133  * on external changes to the privacy-screen status.
3134  * This function takes ownership of the passed in drm_privacy_screen and will
3135  * call drm_privacy_screen_put() on it when the connector is destroyed.
3136  */
3137 void drm_connector_attach_privacy_screen_provider(
3138 	struct drm_connector *connector, struct drm_privacy_screen *priv)
3139 {
3140 	connector->privacy_screen = priv;
3141 	connector->privacy_screen_notifier.notifier_call =
3142 		drm_connector_privacy_screen_notifier;
3143 
3144 	drm_connector_create_privacy_screen_properties(connector);
3145 	drm_connector_update_privacy_screen_properties(connector, true);
3146 	drm_connector_attach_privacy_screen_properties(connector);
3147 }
3148 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_provider);
3149 
3150 /**
3151  * drm_connector_update_privacy_screen - update connector's privacy-screen sw-state
3152  * @connector_state: connector-state to update the privacy-screen for
3153  *
3154  * This function calls drm_privacy_screen_set_sw_state() on the connector's
3155  * privacy-screen.
3156  *
3157  * If the connector has no privacy-screen, then this is a no-op.
3158  */
3159 void drm_connector_update_privacy_screen(const struct drm_connector_state *connector_state)
3160 {
3161 	struct drm_connector *connector = connector_state->connector;
3162 	int ret;
3163 
3164 	if (!connector->privacy_screen)
3165 		return;
3166 
3167 	ret = drm_privacy_screen_set_sw_state(connector->privacy_screen,
3168 					      connector_state->privacy_screen_sw_state);
3169 	if (ret) {
3170 		drm_err(connector->dev, "Error updating privacy-screen sw_state\n");
3171 		return;
3172 	}
3173 
3174 	/* The hw_state property value may have changed, update it. */
3175 	drm_connector_update_privacy_screen_properties(connector, false);
3176 }
3177 EXPORT_SYMBOL(drm_connector_update_privacy_screen);
3178 
3179 int drm_connector_set_obj_prop(struct drm_mode_object *obj,
3180 				    struct drm_property *property,
3181 				    uint64_t value)
3182 {
3183 	int ret = -EINVAL;
3184 	struct drm_connector *connector = obj_to_connector(obj);
3185 
3186 	/* Do DPMS ourselves */
3187 	if (property == connector->dev->mode_config.dpms_property) {
3188 		ret = (*connector->funcs->dpms)(connector, (int)value);
3189 	} else if (connector->funcs->set_property)
3190 		ret = connector->funcs->set_property(connector, property, value);
3191 
3192 	if (!ret)
3193 		drm_object_property_set_value(&connector->base, property, value);
3194 	return ret;
3195 }
3196 
3197 int drm_connector_property_set_ioctl(struct drm_device *dev,
3198 				     void *data, struct drm_file *file_priv)
3199 {
3200 	struct drm_mode_connector_set_property *conn_set_prop = data;
3201 	struct drm_mode_obj_set_property obj_set_prop = {
3202 		.value = conn_set_prop->value,
3203 		.prop_id = conn_set_prop->prop_id,
3204 		.obj_id = conn_set_prop->connector_id,
3205 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
3206 	};
3207 
3208 	/* It does all the locking and checking we need */
3209 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3210 }
3211 
3212 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
3213 {
3214 	/* For atomic drivers only state objects are synchronously updated and
3215 	 * protected by modeset locks, so check those first.
3216 	 */
3217 	if (connector->state)
3218 		return connector->state->best_encoder;
3219 	return connector->encoder;
3220 }
3221 
3222 static bool
3223 drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
3224 			     const struct list_head *modes,
3225 			     const struct drm_file *file_priv)
3226 {
3227 	/*
3228 	 * If user-space hasn't configured the driver to expose the stereo 3D
3229 	 * modes, don't expose them.
3230 	 */
3231 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
3232 		return false;
3233 	/*
3234 	 * If user-space hasn't configured the driver to expose the modes
3235 	 * with aspect-ratio, don't expose them. However if such a mode
3236 	 * is unique, let it be exposed, but reset the aspect-ratio flags
3237 	 * while preparing the list of user-modes.
3238 	 */
3239 	if (!file_priv->aspect_ratio_allowed) {
3240 		const struct drm_display_mode *mode_itr;
3241 
3242 		list_for_each_entry(mode_itr, modes, head) {
3243 			if (mode_itr->expose_to_userspace &&
3244 			    drm_mode_match(mode_itr, mode,
3245 					   DRM_MODE_MATCH_TIMINGS |
3246 					   DRM_MODE_MATCH_CLOCK |
3247 					   DRM_MODE_MATCH_FLAGS |
3248 					   DRM_MODE_MATCH_3D_FLAGS))
3249 				return false;
3250 		}
3251 	}
3252 
3253 	return true;
3254 }
3255 
3256 int drm_mode_getconnector(struct drm_device *dev, void *data,
3257 			  struct drm_file *file_priv)
3258 {
3259 	struct drm_mode_get_connector *out_resp = data;
3260 	struct drm_connector *connector;
3261 	struct drm_encoder *encoder;
3262 	struct drm_display_mode *mode;
3263 	int mode_count = 0;
3264 	int encoders_count = 0;
3265 	int ret = 0;
3266 	int copied = 0;
3267 	struct drm_mode_modeinfo u_mode;
3268 	struct drm_mode_modeinfo __user *mode_ptr;
3269 	uint32_t __user *encoder_ptr;
3270 	bool is_current_master;
3271 
3272 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3273 		return -EOPNOTSUPP;
3274 
3275 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
3276 
3277 	connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
3278 	if (!connector)
3279 		return -ENOENT;
3280 
3281 	encoders_count = hweight32(connector->possible_encoders);
3282 
3283 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
3284 		copied = 0;
3285 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
3286 
3287 		drm_connector_for_each_possible_encoder(connector, encoder) {
3288 			if (put_user(encoder->base.id, encoder_ptr + copied)) {
3289 				ret = -EFAULT;
3290 				goto out;
3291 			}
3292 			copied++;
3293 		}
3294 	}
3295 	out_resp->count_encoders = encoders_count;
3296 
3297 	out_resp->connector_id = connector->base.id;
3298 	out_resp->connector_type = connector->connector_type;
3299 	out_resp->connector_type_id = connector->connector_type_id;
3300 
3301 	is_current_master = drm_is_current_master(file_priv);
3302 
3303 	mutex_lock(&dev->mode_config.mutex);
3304 	if (out_resp->count_modes == 0) {
3305 		if (is_current_master)
3306 			connector->funcs->fill_modes(connector,
3307 						     dev->mode_config.max_width,
3308 						     dev->mode_config.max_height);
3309 		else
3310 			drm_dbg_kms(dev, "User-space requested a forced probe on [CONNECTOR:%d:%s] but is not the DRM master, demoting to read-only probe\n",
3311 				    connector->base.id, connector->name);
3312 	}
3313 
3314 	out_resp->mm_width = connector->display_info.width_mm;
3315 	out_resp->mm_height = connector->display_info.height_mm;
3316 	out_resp->subpixel = connector->display_info.subpixel_order;
3317 	out_resp->connection = connector->status;
3318 
3319 	/* delayed so we get modes regardless of pre-fill_modes state */
3320 	list_for_each_entry(mode, &connector->modes, head) {
3321 		WARN_ON(mode->expose_to_userspace);
3322 
3323 		if (drm_mode_expose_to_userspace(mode, &connector->modes,
3324 						 file_priv)) {
3325 			mode->expose_to_userspace = true;
3326 			mode_count++;
3327 		}
3328 	}
3329 
3330 	/*
3331 	 * This ioctl is called twice, once to determine how much space is
3332 	 * needed, and the 2nd time to fill it.
3333 	 */
3334 	if ((out_resp->count_modes >= mode_count) && mode_count) {
3335 		copied = 0;
3336 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
3337 		list_for_each_entry(mode, &connector->modes, head) {
3338 			if (!mode->expose_to_userspace)
3339 				continue;
3340 
3341 			/* Clear the tag for the next time around */
3342 			mode->expose_to_userspace = false;
3343 
3344 			drm_mode_convert_to_umode(&u_mode, mode);
3345 			/*
3346 			 * Reset aspect ratio flags of user-mode, if modes with
3347 			 * aspect-ratio are not supported.
3348 			 */
3349 			if (!file_priv->aspect_ratio_allowed)
3350 				u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
3351 			if (copy_to_user(mode_ptr + copied,
3352 					 &u_mode, sizeof(u_mode))) {
3353 				ret = -EFAULT;
3354 
3355 				/*
3356 				 * Clear the tag for the rest of
3357 				 * the modes for the next time around.
3358 				 */
3359 				list_for_each_entry_continue(mode, &connector->modes, head)
3360 					mode->expose_to_userspace = false;
3361 
3362 				mutex_unlock(&dev->mode_config.mutex);
3363 
3364 				goto out;
3365 			}
3366 			copied++;
3367 		}
3368 	} else {
3369 		/* Clear the tag for the next time around */
3370 		list_for_each_entry(mode, &connector->modes, head)
3371 			mode->expose_to_userspace = false;
3372 	}
3373 
3374 	out_resp->count_modes = mode_count;
3375 	mutex_unlock(&dev->mode_config.mutex);
3376 
3377 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
3378 	encoder = drm_connector_get_encoder(connector);
3379 	if (encoder)
3380 		out_resp->encoder_id = encoder->base.id;
3381 	else
3382 		out_resp->encoder_id = 0;
3383 
3384 	/* Only grab properties after probing, to make sure EDID and other
3385 	 * properties reflect the latest status.
3386 	 */
3387 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
3388 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
3389 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
3390 			&out_resp->count_props);
3391 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
3392 
3393 out:
3394 	drm_connector_put(connector);
3395 
3396 	return ret;
3397 }
3398 
3399 /**
3400  * drm_connector_find_by_fwnode - Find a connector based on the associated fwnode
3401  * @fwnode: fwnode for which to find the matching drm_connector
3402  *
3403  * This functions looks up a drm_connector based on its associated fwnode. When
3404  * a connector is found a reference to the connector is returned. The caller must
3405  * call drm_connector_put() to release this reference when it is done with the
3406  * connector.
3407  *
3408  * Returns: A reference to the found connector or an ERR_PTR().
3409  */
3410 struct drm_connector *drm_connector_find_by_fwnode(struct fwnode_handle *fwnode)
3411 {
3412 	struct drm_connector *connector, *found = ERR_PTR(-ENODEV);
3413 
3414 	if (!fwnode)
3415 		return ERR_PTR(-ENODEV);
3416 
3417 	mutex_lock(&connector_list_lock);
3418 
3419 	list_for_each_entry(connector, &connector_list, global_connector_list_entry) {
3420 		if (connector->fwnode == fwnode ||
3421 		    (connector->fwnode && connector->fwnode->secondary == fwnode)) {
3422 			drm_connector_get(connector);
3423 			found = connector;
3424 			break;
3425 		}
3426 	}
3427 
3428 	mutex_unlock(&connector_list_lock);
3429 
3430 	return found;
3431 }
3432 
3433 /**
3434  * drm_connector_oob_hotplug_event - Report out-of-band hotplug event to connector
3435  * @connector_fwnode: fwnode_handle to report the event on
3436  * @status: hot plug detect logical state
3437  *
3438  * On some hardware a hotplug event notification may come from outside the display
3439  * driver / device. An example of this is some USB Type-C setups where the hardware
3440  * muxes the DisplayPort data and aux-lines but does not pass the altmode HPD
3441  * status bit to the GPU's DP HPD pin.
3442  *
3443  * This function can be used to report these out-of-band events after obtaining
3444  * a drm_connector reference through calling drm_connector_find_by_fwnode().
3445  */
3446 void drm_connector_oob_hotplug_event(struct fwnode_handle *connector_fwnode,
3447 				     enum drm_connector_status status)
3448 {
3449 	struct drm_connector *connector;
3450 
3451 	connector = drm_connector_find_by_fwnode(connector_fwnode);
3452 	if (IS_ERR(connector))
3453 		return;
3454 
3455 	if (connector->funcs->oob_hotplug_event)
3456 		connector->funcs->oob_hotplug_event(connector, status);
3457 
3458 	drm_connector_put(connector);
3459 }
3460 EXPORT_SYMBOL(drm_connector_oob_hotplug_event);
3461 
3462 
3463 /**
3464  * DOC: Tile group
3465  *
3466  * Tile groups are used to represent tiled monitors with a unique integer
3467  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
3468  * we store this in a tile group, so we have a common identifier for all tiles
3469  * in a monitor group. The property is called "TILE". Drivers can manage tile
3470  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
3471  * drm_mode_get_tile_group(). But this is only needed for internal panels where
3472  * the tile group information is exposed through a non-standard way.
3473  */
3474 
3475 static void drm_tile_group_free(struct kref *kref)
3476 {
3477 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
3478 	struct drm_device *dev = tg->dev;
3479 
3480 	mutex_lock(&dev->mode_config.idr_mutex);
3481 	idr_remove(&dev->mode_config.tile_idr, tg->id);
3482 	mutex_unlock(&dev->mode_config.idr_mutex);
3483 	kfree(tg);
3484 }
3485 
3486 /**
3487  * drm_mode_put_tile_group - drop a reference to a tile group.
3488  * @dev: DRM device
3489  * @tg: tile group to drop reference to.
3490  *
3491  * drop reference to tile group and free if 0.
3492  */
3493 void drm_mode_put_tile_group(struct drm_device *dev,
3494 			     struct drm_tile_group *tg)
3495 {
3496 	kref_put(&tg->refcount, drm_tile_group_free);
3497 }
3498 EXPORT_SYMBOL(drm_mode_put_tile_group);
3499 
3500 /**
3501  * drm_mode_get_tile_group - get a reference to an existing tile group
3502  * @dev: DRM device
3503  * @topology: 8-bytes unique per monitor.
3504  *
3505  * Use the unique bytes to get a reference to an existing tile group.
3506  *
3507  * RETURNS:
3508  * tile group or NULL if not found.
3509  */
3510 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
3511 					       const char topology[8])
3512 {
3513 	struct drm_tile_group *tg;
3514 	int id;
3515 
3516 	mutex_lock(&dev->mode_config.idr_mutex);
3517 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
3518 		if (!memcmp(tg->group_data, topology, 8)) {
3519 			if (!kref_get_unless_zero(&tg->refcount))
3520 				tg = NULL;
3521 			mutex_unlock(&dev->mode_config.idr_mutex);
3522 			return tg;
3523 		}
3524 	}
3525 	mutex_unlock(&dev->mode_config.idr_mutex);
3526 	return NULL;
3527 }
3528 EXPORT_SYMBOL(drm_mode_get_tile_group);
3529 
3530 /**
3531  * drm_mode_create_tile_group - create a tile group from a displayid description
3532  * @dev: DRM device
3533  * @topology: 8-bytes unique per monitor.
3534  *
3535  * Create a tile group for the unique monitor, and get a unique
3536  * identifier for the tile group.
3537  *
3538  * RETURNS:
3539  * new tile group or NULL.
3540  */
3541 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
3542 						  const char topology[8])
3543 {
3544 	struct drm_tile_group *tg;
3545 	int ret;
3546 
3547 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
3548 	if (!tg)
3549 		return NULL;
3550 
3551 	kref_init(&tg->refcount);
3552 	memcpy(tg->group_data, topology, 8);
3553 	tg->dev = dev;
3554 
3555 	mutex_lock(&dev->mode_config.idr_mutex);
3556 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
3557 	if (ret >= 0) {
3558 		tg->id = ret;
3559 	} else {
3560 		kfree(tg);
3561 		tg = NULL;
3562 	}
3563 
3564 	mutex_unlock(&dev->mode_config.idr_mutex);
3565 	return tg;
3566 }
3567 EXPORT_SYMBOL(drm_mode_create_tile_group);
3568